我有以下代码:
import pika
import os
import eventlet
from eventlet.green import threading
pika = eventlet.import_patched('pika')
eventlet.monkey_patch()
#More Code
if __name__=='__main__'
eventlet.spawn(pika_client)
socketio.run(app, host='192.168.1.214')
def pika_client():
global connection, channel
params = pika.ConnectionParameters(heartbeat=500,
blocked_connection_timeout=300)
connection = pika.BlockingConnection(params)
channel = connection.channel()
return 1
但是,pika连接会在20-30分钟后断开连接。
任何帮助将不胜感激。
答案 0 :(得分:0)
Pika的BlockingConnection
与eventlet
修补程序不兼容。 SelectConnection
可以使用的可能性很小,但是从未经过测试并且不受支持。
注意: RabbitMQ团队监视rabbitmq-users
mailing list,并且有时仅在StackOverflow上回答问题。
答案 1 :(得分:0)
我当前正在使用下面的代码,它似乎可以正常工作。请告诉我这是否会造成问题
try:
pikaClient = socketio.start_background_task(pika_client)
socketio.run(app, host='192.168.1.214')
except KeyboardInterrupt:
pikaClient.join()
def pika_client():
global connection, channel
params = pika.ConnectionParameters(heartbeat=600,
blocked_connection_timeout=300)
connection = pika.BlockingConnection(params)
channel = connection.channel()
print('Connection created')
while True:
time.sleep(650)
try:
connection.process_data_events()
except pika.exceptions.StreamLostError:
print("Will try to re-connect next.")
params = pika.ConnectionParameters(heartbeat=600,
blocked_connection_timeout=300)
connection = pika.BlockingConnection(params)
channel = connection.channel()
print('Connection re-created')
#continue
except KeyboardInterrupt:
# Gracefully close the connection
channel.close()
connection.close()