0

I have the following code :

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

However, the pika connection gets disconnected after 20-30 mins.

Any help will be highly appreciated.

Rupak Banerjee
  • 135
  • 1
  • 3
  • 11

2 Answers2

0

Pika's BlockingConnection is not compatible with eventlet patching. There is a small chance that SelectConnection will work, but it has never been tested and is not supported.


NOTE: the RabbitMQ team monitors the rabbitmq-users mailing list and only sometimes answers questions on StackOverflow.

Luke Bakken
  • 8,993
  • 2
  • 20
  • 33
0

I am currently using the code below and it seems to be working.Could you please tell me if this will create problem

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()  
Rupak Banerjee
  • 135
  • 1
  • 3
  • 11