问题描述
我正在开发一个带有后台线程的 Python 应用程序,用于使用来自 RabbitMQ 队列的消息(主题场景).
I'm working on a Python app with a background thread for consuming message from a RabbitMQ Queue (topic scenario).
我在按钮的 on_click 事件上启动线程.这是我的代码,请注意#self.receive_command()".
I start the thread on on_click event of a Button.Here is my code, please take attention on "#self.receive_command()".
def on_click_start_call(self,widget):
t_msg = threading.Thread(target=self.receive_command)
t_msg.start()
t_msg.join(0)
#self.receive_command()
def receive_command(self):
syslog.syslog("ENTERED")
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
syslog.syslog("1")
channel = connection.channel()
syslog.syslog("2")
channel.exchange_declare(exchange='STORE_CMD', type='topic')
syslog.syslog("3")
result = channel.queue_declare(exclusive=True)
syslog.syslog("4")
queue_name = result.method.queue
syslog.syslog("5")
def callback_rabbit(ch,method,properties,body):
syslog.syslog("RICEVUTO MSG: RKEY:"+method.routing_key+" MSG: "+body+"\n")
syslog.syslog("6")
channel.queue_bind(exchange='STORE_CMD', queue=queue_name , routing_key='test.routing.key')
syslog.syslog("7")
channel.basic_consume(callback_rabbit,queue=queue_name,no_ack=True)
syslog.syslog("8")
channel.start_consuming()
如果我运行这段代码,我在系统日志上看不到消息 1,2,3,5,6,7,8 但我只能看到ENTERED".所以,代码被锁定在 pika.BlokingConnection 上.
If i run this code, i can't see on syslog the message 1,2,3,5,6,7,8 But i can see only "ENTERED". So, the code is locked on pika.BlokingConnection.
如果我运行相同的代码(注释线程指令并取消注释对函数的直接调用),所有的工作都按预期进行,消息被正确接收.
If i run the same code(commenting the thread instruction and decommenting the direct call to function), all works as espected and message are correctly received.
有没有办法让消费者运行到线程中?
There are any solutions to run a consumer into a thread?
提前致谢
大卫
推荐答案
我已经在我的机器上测试了代码,使用最新版本的 Pika.它工作正常.Pika 存在线程问题,但只要您为每个线程创建一个连接,就不会有问题.
I have tested the code on my machine, with the latest version of Pika. It works fine. There are threading issues with Pika, but as long as you create one connection per thread it shouldn't be a problem.
如果您遇到问题,很可能是因为旧版 Pika 中的错误,或者与您的线程无关的问题导致了问题.
If you are experiencing issues, it is most likely because of a bug in an older version of Pika, or an unrelated issues with your threading causing an issue.
我建议您避免使用 0.9.13,因为存在多个错误,但 0.10.0 应该很快发布™.
I would recommend that you avoid 0.9.13 as there are multiple bugs, but 0.10.0 should be released very soon™.
鼠兔 0.9.14 已发布.
这是我使用的代码.
def receive_command():
print("ENTERED")
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
print("1")
channel = connection.channel()
print("2")
channel.exchange_declare(exchange='STORE_CMD', type='topic')
print("3")
result = channel.queue_declare(exclusive=True)
print("4")
queue_name = result.method.queue
print("5")
def callback_rabbit(ch,method,properties,body):
print("RICEVUTO MSG: RKEY:"+method.routing_key+" MSG: "+body+"\n")
print("6")
channel.queue_bind(exchange='STORE_CMD', queue=queue_name , routing_key='test.routing.key')
print("7")
channel.basic_consume(callback_rabbit,queue=queue_name,no_ack=True)
print("8")
channel.start_consuming()
def start():
t_msg = threading.Thread(target=receive_command)
t_msg.start()
t_msg.join(0)
#self.receive_command()
start()
这篇关于Python Pika - 消费者进入线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!