Пример #1
0
    def __init__(self, transport, codec=Codec()):
        self.__log = logging.getLogger("Broker")
        threading.Thread.__init__(self)
        self.__minimal = Minimal(codec=codec, transport=transport)
        self.__rqueue = Queue(1)
        self.__wqueue = Queue(1)
        self.__callback_lock = threading.RLock()

        self.__callbacks = {
            'TOPIC': {},
            'QUEUE': {},
            'POLL': {},
            'PONG': {},
        }

        def __rloop():
            while True:
                self.__log.debug('__rloop reading')
                message = self.__minimal.receive()
                self.__log.debug('__rloop read %r', message)
                self.__rqueue.put(message)

        def __wloop():
            while True:
                self.__log.debug('__wloop reading')
                message = self.__wqueue.get()
                self.__log.debug('__wloop read %r', message)
                self.__minimal.send(message)
                self.__log.debug('__wloop sent %r', message)
                self.__wqueue.task_done()

        self.__rthread = threading.Thread(target=__rloop, name='broker_r')
        self.__rthread.setDaemon(True)

        self.__wthread = threading.Thread(target=__wloop, name='broker_w')
        self.__wthread.setDaemon(True)

        self.setDaemon(True)

        #start threads
        self.__log.debug('Starting threads')
        self.__rthread.start()
        self.__wthread.start()
        self.__log.debug('Threads started')
#!/usr/bin/env python

from Broker.Messages import Message, Publish, Authentication
from Broker.Transport import TCP, UDP, SSL
from Broker.Codecs import Protobuf as Codec  #auto codec selection (thrift or protobuf if thrift isn't installed)
from Broker.Clients import Minimal

server = '127.0.0.1'
destination = '/python/tests'
destination_type = 'QUEUE'
N = 1000

broker = Minimal(codec=Codec(), transport=SSL(host=server))

broker.send(
    Authentication.from_sts_credentials(username='******',
                                        password='******'))

for n in xrange(N):
    message = Message(payload='Message number %d' % n)
    publish = Publish(destination=destination,
                      destination_type=destination_type,
                      message=message)
    broker.send(publish)