Domanda

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 ?

È stato utile?

Soluzione

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

eandersson created an example of this here.

Altri suggerimenti

If you want use basic_cancel see this gist

you can see code briefly like below:

def callback(ct, ch, method, properties, body):
   ...
   ch.basic_cancel(consumer_tag=ct, nowait=False) # WARNING no such parameter `nowait` please remove from method call
   ...

...
consumer_tag = uuid.uuid1().hex
channel.basic_consume(partial(callback, consumer_tag),
                              queue=queue_name,
                              consumer_tag=consumer_tag) # add consumer_tag
...

in pika@1.3.3 there is no such parameter named nowait, so you just remove it from method call.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top