我有两个python进程,一个消费者进程和一个生产者进程。每个进程将启动一个rabbitmq连接并生成多个使用者/生产者线程。每个线程将在连接中创建一个通道,并执行消息发送和接收逻辑。在
这是我的消费线索
def consumer_thread(connection, routing_key):
channel = connection.channel()
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange="test", routing_key=routing_key, queue=queue_name)
thread_name = current_thread().name
def process(ch, method, properties, body):
print(f"{thread_name} received {body}")
channel.basic_consume(process, queue=queue_name, no_ack=True)
channel.start_consuming()
这是我的制作人线索
^{pr2}$我用启动一个rabbitmq连接
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
然而,当我第一次运行这个消息时,我收到了这个消息
Traceback (most recent call last):
File "D:\app\cortex-bin\Python36\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "D:\app\cortex-bin\Python36\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "D:\app\cortex\background\core\scratch\test.py", line 18, in consumer_thread
channel.start_consuming()
File "D:\app\cortex-bin\Python36\lib\site-packages\pika\adapters\blocking_connection.py", line 1817, in start_consuming
'start_consuming may not be called from the scope of 'pika.exceptions.RecursionError: start_consuming may not be called from the scope of another BlockingConnection or BlockingChannel callback'
对于所有后续的消息,消费者线程可以很好地接收它们。在
我能知道是什么引起了这个异常吗?谢谢。在
不能从多个线程(comment)访问Pika连接。你的线程必须启动自己的连接和通道。在
注意:RabbitMQ团队监控the ^{} mailing list ,有时只回答StackOverflow的问题。
相关问题 更多 >
编程相关推荐