gmr / rabbitpy

A pure python, thread-safe, minimalistic and pythonic RabbitMQ client library
http://rabbitpy.readthedocs.org
BSD 3-Clause "New" or "Revised" License
242 stars 58 forks source link

proper closing with multiple queues #117

Closed heycalmdown closed 5 years ago

heycalmdown commented 5 years ago

I need multiple threads to consume multiple queues. Channels can not be shared through threads, so the each thread should make their own.

def thread_rpc_queue(connection, queueName):
    with connection.channel() as ch:
        q = rabbitpy.Queue(ch, queueName)
        q.declare()

        for msg in q:
            print('yay')
            msg.ack()

for i in range(10):
    t = threading.Thread(target=thread_rpc_queue, kwargs={'connection': conn, 'queueName': 'q.' + str(i)})
    t.start()

But because of conn.close() will delete its channels in the main thread, I'm getting several errors like by random;

Could you give me some advice how to handle multiple queues and close properly?

DanSchum commented 5 years ago

Good question. Facing similar problem. Any updates?

gmr commented 5 years ago

Create the connection directly without using it as a context manager:

conn = rabbitpy.Connection(...)

Then pass that in your threads and create your channel in the thread.

DanSchum commented 5 years ago

I saved all my active queues (from different threads) and before closing the connection I cancel the consuming of these queue gracefully. This works pretty good for me.