def on_kernels_message(self, body, message): print("notify: RECEIVED KERNELS MSG - body: %r" % (body, )) print("notify: RECEIVED KERNELS MSG - message: %r" % (message, )) message.ack() try: respTarget = body['Source'] respQueue = body['RespQueue'] method = body['Method'] args = body['args'] # create the response connection resp_connection = BrokerConnection(respTarget) resp_queue = resp_connection.SimpleQueue(respQueue, queue_opts={ 'durable': False, 'auto_delete': True }, exchange_opts={ 'delivery_mode': 1, 'auto_delete': True, 'durable': False }) payload = {"Result": DispatchKernels(method, args)} resp_queue.put(payload, serializer='json') resp_queue.close() except: print "Exception caught : %s" % sys.exc_info()[0] return
def wait_many(timeout=1): #: Create connection #: If hostname, userid, password and virtual_host is not specified #: the values below are the default, but listed here so it can #: be easily changed. connection = BrokerConnection("amqp://*****:*****@localhost:5672//") #: SimpleQueue mimics the interface of the Python Queue module. #: First argument can either be a queue name or a kombu.Queue object. #: If a name, then the queue will be declared with the name as the queue #: name, exchange name and routing key. queue = connection.SimpleQueue("kombu_demo") while True: try: message = queue.get(block=False, timeout=timeout) except Empty: break else: spawn(message.ack) print(message.payload) queue.close() connection.close()
def send_many(n): #: Create connection #: If hostname, userid, password and virtual_host is not specified #: the values below are the default, but listed here so it can #: be easily changed. connection = BrokerConnection(hostname="localhost", userid="guest", password="******", virtual_host="/") #: SimpleQueue mimics the interface of the Python Queue module. #: First argument can either be a queue name or a kombu.Queue object. #: If a name, then the queue will be declared with the name as the queue #: name, exchange name and routing key. queue = connection.SimpleQueue("kombu_demo") def send_message(i): queue.put({"hello": "world%s" % (i, )}) pool = eventlet.GreenPool(10) for i in xrange(n): pool.spawn(send_message, i) pool.waitall()
def CallServer(self, method, args=None): try: LOG.debug(_("strBroker : %s "), self._strBroker) connection = BrokerConnection(self._strBroker) # create the response channel respQueueName = self._respQueueName + str(uuid()) respconnection = BrokerConnection(self._strBroker) respQueue = respconnection.SimpleQueue(respQueueName, queue_opts={ 'durable': False, 'auto_delete': True }, exchange_opts={ 'delivery_mode': 1, 'auto_delete': True, 'durable': False }) with producers[connection].acquire(block=True) as producer: maybe_declare(task_exchange, producer.channel) payload = { "RespQueue": respQueueName, "Source": self._strBroker, 'Method': method, 'args': args } producer.publish(payload, exchange=self._exchange, serializer="json", routing_key=self._routing_key) # wait for the response resp_message = respQueue.get(block=True, timeout=1) resp_message.ack() respQueue.close() #respQueue.delete() except: LOG.debug(_("Exception caught : %s"), sys.exc_info()[0]) raise OpenCLClientException.OpenCLClientException( "OpenCL Interface Exception") if type(resp_message.payload['Result']).__name__ in ('list', 'tuple'): nElems = len(resp_message.payload['Result']) if resp_message.payload['Result'][nElems - 1] == -128: raise OpenCLClientException.OpenCLClientException( "OpenCL Interface Exception") elif type(resp_message.payload['Result']).__name__ == 'int': if resp_message.payload['Result'] == -128: raise OpenCLClientException.OpenCLClientException( "OpenCL Interface Exception") else: raise OpenCLClientException.OpenCLClientException( "OpenCL Interface Exception") return resp_message.payload['Result']
""" Example receiving a message using the SimpleQueue interface. """ from kombu import BrokerConnection #: Create connection #: If hostname, userid, password and virtual_host is not specified #: the values below are the default, but listed here so it can #: be easily changed. connection = BrokerConnection(hostname="localhost", userid="guest", password="******", virtual_host="/") #: SimpleQueue mimics the interface of the Python Queue module. #: First argument can either be a queue name or a kombu.Queue object. #: If a name, then the queue will be declared with the name as the queue #: name, exchange name and routing key. queue = connection.SimpleQueue("kombu_demo") message = queue.get(block=True, timeout=10) message.ack() print(message.payload)
from kombu import BrokerConnection from Queue import Empty connection = BrokerConnection("amqp://*****:*****@localhost:5672//") queue = connection.SimpleQueue("logs") while 1: try: message = queue.get(block=True, timeout=1) print message.payload message.ack() except Empty: pass print message queue.close()