Rabbitmq 使用Pika BlockingConnection时,是否必须将basic_ack()放在回调函数中

Rabbitmq 使用Pika BlockingConnection时,是否必须将basic_ack()放在回调函数中,rabbitmq,multiprocessing,pika,Rabbitmq,Multiprocessing,Pika,假设我已连接到RabbitMQ,如下所示: connection = pika.BlockingConnection(pika.ConnectionParameters( 'localhost', 5672, '/', credentials)) channel = connection.channel() channel.queue_declare(queue=getting_from_this_queue) channel.basic_consume( callback, q

假设我已连接到RabbitMQ,如下所示:

connection = pika.BlockingConnection(pika.ConnectionParameters(
    'localhost', 5672, '/', credentials))
channel = connection.channel()
channel.queue_declare(queue=getting_from_this_queue)
channel.basic_consume(
    callback, queue=getting_from_this_queue, no_ack=False)
channel.basic_qos( prefetch_count = 3 )
为了实现更好的并发性,我尝试将每个作业放置在内部队列中,并创建了一个while循环,以便为从该内部队列检索到的每个作业异步分派工作进程:

from Queue import Queue
from multiprocessing.dummy import Pool as ThreadPool

task_queue = Queue(10)
pool = Pool(20)

def worker(ch, method, job):
    # ...some heavy lifting...
     if job_gets_done:         # some abstraction
        print "job success"
        ch.basic_ack(delivery_tag=method.delivery_tag)   # PROBLEM : this seems not working
     else:
        print "job failed"

def callback(ch, method, properties, job):
     task_queue.put((ch,method,dn))     # put job in internal queue, block if full.

@threaded
def async_process_jobs():              # loop to get job and start thread worker.
    while True:
         params = task_queue.get()
         pool.apply_async( worker, params )   # param = (ch,method, job)


async_process_jobs()
channel.start_consuming()
问题是,在处理作业时,没有一个作业正确地发送确认(即使执行流真的通过它,即打印“作业成功”)。rabbitmq上的队列大小保持不变,为什么

在a中,基本的_ack()放在callback()中,但我的没有。这可能是问题的根源吗


详细行为(可能不重要):假设队列中有10000个作业,开始时,大约2000条消息进入未确认状态,然后所有消息都返回就绪状态,即使我的工作人员仍在处理和打印“作业成功”(确认)。

来自:

Pika在代码中没有任何线程概念。如果你想 使用带线程的Pika,确保每个线程都有Pika连接 线程,在该线程中创建。分享一只鼠兔是不安全的 跨线程的连接


我遇到了类似的问题,我注意到: 如果工作完成得很快,则ack工作正常
但是如果作业花费更多的时间,那么ack就不起作用,即使它发送出去。

顺便说一句,Python有许多类似的库,它们实际上支持线程;e、 拉比。