Search code examples
pythonrabbitmqamqppika

Right way to cancel rabbitmq consuming with pika


I want to implement rpc client using rabbitmq. My code primarily looks like

def start(self):
    while True:
        self.channel.basic_consume(self.on_delivery)
 ...
 client.start() // blocking call

What is right way to stop this client ? Now I make channel.basic_cancel(self.on_cancel, self.consumer_tag) from another thread. However pika faq says It is not safe to share one Pika connection across threads.

What is preferred way to cancel consuming ?


Solution

  • As long as you create one connection per thread, you should be fine.

    eandersson created an example of this here.