我在和兔子一起工作。代码被卡在channel.start_consuming()上。队列中有消息。
有什么问题吗。当我强制使用ctrl+C结束代码时:
INFO:pika.adapters.base_connection:Connecting fd 4 to localhost:5672
INFO:pika.adapters.blocking_connection:Adapter connected
[*] Waiting for messages. To exit press CTRL+C
^CTraceback (most recent call last):
File "recipe_code.py", line 293, in <module>
channel.start_consuming()
File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 722, in start_consuming
self.connection.process_data_events()
File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 88, in process_data_events
if self._handle_read():
File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 184, in _handle_read
super(BlockingConnection, self)._handle_read()
File "/usr/local/lib/python2.7/dist-packages/pika/adapters/base_connection.py", line 296, in _handle_read
data = self.socket.recv(self._buffer_size)
KeyboardInterruptrecipe_code.py是一名工人:
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='myqueue', durable=True)
print ' [*] Waiting for messages. To exit press CTRL+C'
def callback(ch, method, properties, body):
//do some work//
ch.basic_ack(delivery_tag = method.delivery_tag)
channel.basic_qos(prefetch_count=1)
channel.basic_consume(callback,queue='myqueue')
channel.start_consuming()发布于 2015-03-01 18:47:40
我在代码中看到的唯一问题是basic_qos和basic_consume的缩进。如果您发布的代码是正确的,那么这两个函数将永远不会调用。
connection = \
pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
def callback(ch, method, properties, body):
print "Message:", body
ch.basic_ack(delivery_tag=method.delivery_tag)
channel.queue_declare(queue='myqueue', durable=True)
# You had an unwanted indent here.
channel.basic_qos(prefetch_count=1)
channel.basic_consume(callback, queue='myqueue')
print ' [*] Waiting for messages. To exit press CTRL+C'
channel.start_consuming()您拥有的打印消息也应该在start_consuming线上,因为这时pika实际上将开始监听要消费的消息。
https://stackoverflow.com/questions/28713820
复制相似问题