gpt4 book ai didi

python - RabbitMQ pika.exceptions.ConnectionClosed

转载 作者:太空狗 更新时间:2023-10-29 21:30:55 28 4
gpt4 key购买 nike

我尝试使用 RabbitMQ 发送消息和接收消息。我没有计算机科学背景,我使用的术语可能不是很准确。

我尝试复制教程文件:提交我的 html 表单时,我的 python 脚本 (cgi) 将消息提交到队列

connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='task_queue', durable=True)
message = PN
channel.basic_publish(exchange='',
routing_key='task_queue',
body=message,
properties=pika.BasicProperties(
delivery_mode = 2, # make message persistent
))
connection.close()

我的接收器正在运行:

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 Project %r" % body)
#ch.basic_ack(delivery_tag = method.delivery_tag)
if not (os.path.isfile(js_path)):
print (' [*] ERROR files missing ')
#ch.basic_ack(delivery_tag = method.delivery_tag)
return
p= subprocess.Popen(run a subprocess here)
p.wait()

print (' [*] Temporary Files removed')
print(" [*] Waiting for messages. To exit press CTRL+C")

channel.basic_qos(prefetch_count=1)
channel.basic_consume(callback,queue='task_queue',no_ack=True)
channel.start_consuming()

它管理大部分时间,但随机崩溃并出现以下错误:

Traceback (most recent call last): File "Receive5.py", line 139, in channel.start_consuming() File "C:\Python27\lib\site-packages\pika\adapters\blocking_connection.py", line 1681, in start_consuming self.connection.process_data_events(time_limit=None) File "C:\Python27\lib\site-packages\pika\adapters\blocking_connection.py", line 647, in process_data_events self._flush_output(common_terminator) File "C:\Python27\lib\site-packages\pika\adapters\blocking_connection.py", line 426, in _flush_output raise exceptions.ConnectionClosed() pika.exceptions.ConnectionClosed

最佳答案

这是因为你让主线程一直在等待,因此 pika 无法处理传入的消息;在这种情况下,在子进程完成之前它无法响应心跳。这导致 RabbitMQ 认为客户端已死并强制断开连接。

如果您希望它与心跳一起工作(推荐),您需要定期调用 connection.process_data_events。这可以通过添加一个循环来检查线程是否完成,并且每 30 秒左右调用一次 process_data_events 直到线程完成。

关于python - RabbitMQ pika.exceptions.ConnectionClosed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37321089/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com