首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rabbitmq: channel.start_consuming()不会消耗

Rabbitmq: channel.start_consuming()不会消耗
EN

Stack Overflow用户
提问于 2015-02-25 07:51:34
回答 1查看 3.7K关注 0票数 2

我在和兔子一起工作。代码被卡在channel.start_consuming()上。队列中有消息。

有什么问题吗。当我强制使用ctrl+C结束代码时:

代码语言:javascript
复制
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)
KeyboardInterrupt

recipe_code.py是一名工人:

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-01 18:47:40

我在代码中看到的唯一问题是basic_qosbasic_consume的缩进。如果您发布的代码是正确的,那么这两个函数将永远不会调用。

代码语言:javascript
复制
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实际上将开始监听要消费的消息。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28713820

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档