rabbitmq Pika连接频繁断开

wgmfuz8q  于 2023-10-20  发布在  RabbitMQ
关注(0)|答案(2)|浏览(204)

我有以下代码:

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

然而,鼠兔连接在20-30分钟后断开。
任何帮助将不胜感激。

nnvyjq4y

nnvyjq4y1#

我目前正在使用下面的代码,它似乎是工作。你能告诉我,如果这将创建问题

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()
kyxcudwk

kyxcudwk2#

Pika的BlockingConnectioneventlet补丁不兼容。SelectConnection工作的可能性很小,但它从未经过测试,也不受支持。

相关问题