【发布时间】:2015-10-11 14:10:20
【问题描述】:
我有 N 个工作人员使用我的 RabbitMQ 任务。但我希望他们能够同时处理多个任务。
我读到了 prefetch_count 参数,它可以让我这样做,但它不起作用。
这是我的 Python 代码,使用 Pika 库:
import pika
import time
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='task_queue', durable=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
def callback(ch, method, properties, body):
print(" [x] Received %r" % (body,))
time.sleep(body.count(b'.'))
print(" [x] Done")
ch.basic_ack(delivery_tag=method.delivery_tag)
channel.basic_qos(prefetch_count=2) # Here I should be able to handle 2 tasks in the same time
channel.basic_consume(callback,
queue='task_queue')
channel.start_consuming()
不幸的是,如果当前正在处理一个任务(因此,basic_ack 尚未发送),则不会为第二个任务调用回调。它等待当前任务完成后开始下一个任务。
【问题讨论】:
-
你是如何运行 N 个工人的。我有相同的要求,我想运行 10 名工人。该怎么做?
标签: python queue rabbitmq task