Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用amqp库创建非阻塞消费者_Python_Multithreading_Rabbitmq_Amqp - Fatal编程技术网

Python 如何使用amqp库创建非阻塞消费者

Python 如何使用amqp库创建非阻塞消费者,python,multithreading,rabbitmq,amqp,Python,Multithreading,Rabbitmq,Amqp,我将amqp模块与RabbitMQ一起使用。我需要创建一个非阻塞消费者。为此,我使用了线程模块 有一个问题:我必须使线程在应用程序退出时停止。以下是代码的一部分: c.start_consuming(message_callback) while not self._stop.isSet(): if self._stop.isSet(): print("thread will be stopped") else: print("thread w

我将amqp模块与RabbitMQ一起使用。我需要创建一个非阻塞消费者。为此,我使用了线程模块

有一个问题:我必须使线程在应用程序退出时停止。以下是代码的一部分:

c.start_consuming(message_callback)

while not self._stop.isSet():

    if self._stop.isSet():
        print("thread will be stopped")
    else:
        print("thread will NOT BE STOPPED")

    c.channel.wait()
问题是,c.channel.wait()有时阻塞,有时不依赖于队列中是否有它正在侦听的消息(我做了一些实验,但还不够)

如果有一个超时参数可以与c.channel.wait()函数一起使用,我可以通过设置超时来实现这个目标,例如0.1秒。就我搜索源代码而言,没有超时选项

主要问题:如何使用amqp模块创建非阻塞消费者

子问题1:如何修补amqp代码,使其开始使用超时值


回退解决方案:我可以考虑使用多处理模块,以便随时消灭该过程。 在c频道上向其发送“立即终止”消息。