[rabbitmq-discuss] Publish on a queue, then Monitor on a reply queue, with only 1 connection to the RabbitMQ server? Non-blocking?

Christopher Lefevre CLefevre at Transparent.com
Tue Sep 6 17:19:56 BST 2011


To note, if this is an easier description, I’m trying to implement the RPC example on the RabbitMQ tutorial with a synchronous connection. Which I’ve copied below so you guys don’t need to go find it. However, the main impetus to me moving forward is lines 13/14:

        result = self.channel.queue_declare(exclusive=True)
        self.callback_queue = result.method.queue



Line 13 declares the result variable as the product of the channel getting an exclusive queue created, which I get. However the part that I get caught up in is where the callback is set to result.method.queue in line 14. What exactly is the method.queue of the result object? I am hoping to simply modify my Monitor/Publish classes to be able to have a synchronous publish/monitor without creating 2 connections. My current attempt at simply using one connection with 2 channels does not seem to be operating how I expected.

#!/usr/bin/env python
import pika
import uuid

class FibonacciRpcClient(object):
    def __init__(self):
        self.connection = pika.BlockingConnection(pika.ConnectionParameters(
                host='localhost'))

        self.channel = self.connection.channel()

        result = self.channel.queue_declare(exclusive=True)
        self.callback_queue = result.method.queue

        self.channel.basic_consume(self.on_response, no_ack=True,
                                   queue=self.callback_queue)

    def on_response(self, ch, method, props, body):
        if self.corr_id == props.correlation_id:
            self.response = body

    def call(self, n):
        self.response = None
        self.corr_id = str(uuid.uuid4())
        self.channel.basic_publish(exchange='',
                                   routing_key='rpc_queue',
                                   properties=pika.BasicProperties(
                                         reply_to = self.callback_queue,
                                         correlation_id = self.corr_id,
                                         ),
                                   body=str(n))
        while self.response is None:
            self.connection.process_data_events()
        return int(self.response)

fibonacci_rpc = FibonacciRpcClient()

print " [x] Requesting fib(30)"
response = fibonacci_rpc.call(30)
print " [.] Got %r" % (response,)



-Christopher Lefevre

From: Michael Klishin [mailto:michael.s.klishin at gmail.com]
Sent: Tuesday, September 06, 2011 9:38 AM
To: Christopher Lefevre
Cc: rabbitmq-discuss at lists.rabbitmq.com
Subject: Re: [rabbitmq-discuss] Publish on a queue, then Monitor on a reply queue, with only 1 connection to the RabbitMQ server? Non-blocking?

2011/9/6 Christopher Lefevre <CLefevre at transparent.com<mailto:CLefevre at transparent.com>>
Though I also wanted it to be non-blocking so the website isn’t stuck waiting for the reply, but rather can be updated as needed. Currently, I can get it to work if there is a pause, like when stepping through the program in a debugger, but when it runs without myself stepping through, it simply hangs when the queue is created and the message is never picked up.

Please post a code example that is at least similar to your actual code.

--
MK

http://github.com/michaelklishin
http://twitter.com/michaelklishin
The information contained in this electronic message and any attached document(s) is intended only for the personal and confidential use of the designated recipients named above. This message may be confidential. If the reader of this message is not the intended recipient, you are hereby notified that you have received this document in error, and that any review, dissemination, distribution, or copying of this message is strictly prohibited. If you have received this communication in error, please notify sender immediately by telephone (603) 262-6300 or by electronic mail immediately. Thank you.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20110906/82f314be/attachment.htm>


More information about the rabbitmq-discuss mailing list