6

So far for a single queue in RabbitMQ I have used a single channel But now I have multiple queues created dynamically, so do I have to create a new channel for each queue or one channel can be to receive/send messages from/to different queues?

   # consuming
    for ch in items:
      channel1 = rconn.channel()
      channel1.queue_declare(queue=itm)
      channel1.basic_consume(some_callback, queue=itm, no_ack=True)
      channel1.start_consuming()


    # publishing
    for ch in items:
    # ....
      channel1.basic_publish(exchange="", routing_key=itm, body="fdsfds")
Alan Coromano
  • 24,958
  • 53
  • 135
  • 205

2 Answers2

1

I've had weird issues when I tried to reuse the channel. I'd go with multiple channels. One per each type of producer/consumer is what I ended using iirc.

JVXR
  • 1,294
  • 1
  • 11
  • 20
  • could you show me the code because when I call "start_consuming()" it goes into an infinite loop as it should, so how can I setup multiple channels? – Alan Coromano Feb 12 '16 at 00:24
  • To clarify, I had different kinds of message processors in an app, and I was trying to maintain a single channel and share it between the consumers and stuff used to hit the fan. This was a few years back :) – JVXR Feb 12 '16 at 02:42
  • sorry, that won't help me to understand how to do that. – Alan Coromano Feb 12 '16 at 02:47
0

You do not need to have one queue per channel. You can both declare and consume from multiple queues on the same channel. See this question for more info.

In many client libraries, the queue declaration "RPC" operations should not be mixed with the consume "streaming" operations. In such cases, it's better to have two channels: one for any number of RPC things like queue declarations, deletions, binding creation, etc., and one for any number of consumes.

I think the official Python driver handles this correctly and does not require more than one channel for both.

To (very roughly and nondeterministically) test this, start a publisher somewhere sending a steady stream of messages to a queue, and create a consumer on that queue that consumes messages while repeatedly declaring other queues. If everything works well for awhile, your client is fine mixing RPC and streaming operations. Of course, the client's documentation on the subject is a better authority than this test.

Zac B
  • 3,796
  • 3
  • 35
  • 52
  • you mean multiple consumers can consume messages from multiple queues using the same channel? – lily May 10 '20 at 14:42
  • @Zac B Your answer created confusion with the thread below https://stackoverflow.com/questions/52351459/multiple-queues-consuming-in-one-channel – Somnath Sep 22 '21 at 18:48