[rabbitmq-discuss] Problem extending code from tutorial #6
Doug Gorley
doug at gorley.ca
Wed Aug 24 14:32:17 BST 2011
Made the change as per your update and it works great. Thanks Marek!
--
Doug Gorley | doug at gorley.ca
On Wed, August 24, 2011 3:52 am, Marek Majkowski wrote:
> On Wed, Aug 24, 2011 at 09:32, Marek Majkowski <majek04 at gmail.com> wrote:
>> Thanks for the report. It does look like a problem caused
>> by a bug in Pika or my misunderstanding how start/stop_consuming
>> work.
>
> Yup. I did misuse "stop_consuming". Apparently, it calls
> basic.cancel, which was completely not inteded:
> https://github.com/pika/pika/blob/v0.9.5/pika/adapters/blocking_connection.py#L287
>
> The fix is here:
> https://github.com/rabbitmq/rabbitmq-tutorials/commit/c1b178d9deec664cdf12770c24319c7d12d67efd
>
> Thanks again for the report!
>
> Cheers,
> Marek
>
>> On Wed, Aug 24, 2011 at 02:49, Doug Gorley <doug at gorley.ca> wrote:
>>> I'm hoping someone can help he with my RabbitMQ problem; I've got some
>>> code based on the tutorial #6 code, and it's not doing what I'm
>>> expecting.
>>> I tried to create a shell where I can send strings to the server and
>>> receive the MD5 digest, but I only ever seem to get the first reply.
>>>
>>> Here's the source:
>>>
>>> ##########################################################################
>>> # server.py
>>>
>>> import pika
>>> import hashlib
>>>
>>> connection = pika.BlockingConnection(
>>> Â Â pika.ConnectionParameters(host='localhost'))
>>>
>>> channel = connection.channel()
>>> channel.queue_declare(queue='my_request')
>>>
>>> def on_request(ch, method, props, body):
>>>
>>> Â Â response = hashlib.md5(body).hexdigest()
>>>
>>> Â Â print("Received '%r', sending '%r'" % (body, response))
>>>
>>> Â Â ch.basic_publish(exchange='',
>>> Â Â Â Â Â Â Â Â Â Â routing_key=props.reply_to,
>>> Â Â Â Â Â Â Â Â Â Â
>>> properties=pika.BasicProperties(correlation_id = \
>>> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
>>> Â Â props.correlation_id),
>>> Â Â Â Â Â Â Â Â Â Â body=response)
>>>
>>> Â Â ch.basic_ack(delivery_tag = method.delivery_tag)
>>>
>>> channel.basic_qos(prefetch_count=1)
>>> channel.basic_consume(on_request, queue='my_request')
>>>
>>> print " [x] Awaiting requests"
>>> channel.start_consuming()
>>> ##########################################################################
>>>
>>> ##########################################################################
>>> # client.py
>>>
>>> import pika
>>> import uuid
>>>
>>> class RabbitClient(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):
>>> Â Â Â Â print(" [.] Received correlation ID %r" %
>>> (props.correlation_id,))
>>> Â Â Â Â if self.corr_id == props.correlation_id:
>>> Â Â Â Â Â Â self.response = body
>>> Â Â Â Â Â Â self.channel.stop_consuming()
>>> Â Â Â Â else:
>>> Â Â Â Â Â Â print(" [~] Correlation IDs do not match")
>>>
>>> Â Â def call(self, n):
>>> Â Â Â Â self.corr_id = str(uuid.uuid4())
>>> Â Â Â Â self.channel.basic_publish(exchange='',
>>> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
>>> routing_key='my_request',
>>> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
>>> properties=pika.BasicProperties(
>>> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
>>> reply_to=self.callback_queue,
>>> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
>>> correlation_id=self.corr_id,
>>> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ),
>>> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â body=n)
>>> Â Â Â Â self.channel.start_consuming()
>>> Â Â Â Â return self.response
>>>
>>> auth = RabbitClient()
>>>
>>> while True:
>>> Â Â send = raw_input('> ')
>>> Â Â if send == 'quit': break
>>> Â Â print(" [x] sending '%s'" % (send))
>>> Â Â response = auth.call(send)
>>> Â Â print(" [^] sent with correlation ID %s" % (auth.corr_id,))
>>> Â Â print(" [.] Got %r" % (response,))
>>> ##########################################################################
>>>
>>> Here's the output from the server:
>>>
>>> ##########################################################################
>>> Â [x] Awaiting requests
>>> Received ''Hello World'', sending ''b10a8db164e0754105b7a99be72e3fe5''
>>> Received ''Hello Rabbit'', sending ''5bec01f45b43f6202e84242fcb71b04f''
>>> ##########################################################################
>>>
>>> And here's the output from the server:
>>> ##########################################################################
>>>> Hello World
>>> Â [x] sending 'Hello World'
>>> Â [.] Received correlation ID '1805f131-1fbc-40a3-86fb-2a113b5f9b9a'
>>> Â [^] sent with correlation ID 1805f131-1fbc-40a3-86fb-2a113b5f9b9a
>>> Â [.] Got 'b10a8db164e0754105b7a99be72e3fe5'
>>>> Hello Rabbit
>>> Â [x] sending 'Hello Rabbit'
>>> Â [^] sent with correlation ID fdb089ff-44ea-4b81-885b-be7e0178d5a1
>>> Â [.] Got 'b10a8db164e0754105b7a99be72e3fe5'
>>>> quit
>>> ##########################################################################
>>>
>>> So, my question is, what am I doing wrong that I didn't get the '5bec'
>>> response to the second message?
>>>
>>> Thanks for your help,
>>> --
>>> Doug Gorley | doug at gorley.ca
>>>
>>>
>>>
>>> _______________________________________________
>>> rabbitmq-discuss mailing list
>>> rabbitmq-discuss at lists.rabbitmq.com
>>> https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss
>>>
>>
>
More information about the rabbitmq-discuss
mailing list