[rabbitmq-discuss] Help with topics not working from Pika

Simon MacMullen simon at rabbitmq.com
Wed Sep 4 14:05:00 BST 2013


Because both receivers declare a queue with the same name, they both get 
the same queue. Each message can only exist once in a queue, so 
therefore you distribute the messages between the two receivers.

To get each (matching) message routed to each receiver, each receiver 
needs its own queue, and hence its own queue name. The simplest way to 
do this is with server-named queues as discussed in tutorial 3.

Cheers, Simon

On 04/09/13 13:57, Darren Govoni wrote:
> Hi,
>    I have a bizarre situation with topics working and not working in
> pika. Pretty sure this is a bug of some kind.
> I adapted the topic pub/sub example slightly and found that it doesn't
> act like a topic anymore, but a direct queue.
>
> Clearly these two files establish a topic exchange and bind a queue to
> it and should thus behave like a topic,
> but it behaves like a direct queue only.
>
> Step 1. In one window run "python receive_1.py _workflow"
> Step 2. In another window run "python receive_1.py _workflow"
> Step 3. In a third window run "python emit_1.py _workflow"
>
> Only one of the receivers gets the message. If you continue to run
> emit_1.py
> it distributes between your two receivers.
>
> Can I not declare a queue name and bind it to a topic exchange? It seems
> not.
>
> Here are the files:
>
> emit_1.py
> ============================================
> #!/usr/bin/env python
> import pika
> import sys
>
> connection = pika.BlockingConnection(pika.ConnectionParameters(
>          host='localhost'))
> channel = connection.channel()
>
> channel.exchange_declare(exchange='workflow',
>                           type='topic')
>
> routing_key = sys.argv[1] if len(sys.argv) > 1 else 'anonymous.info'
> message = ' '.join(sys.argv[2:]) or 'Hello World!'
> channel.basic_publish(exchange='workflow',
>                        routing_key=routing_key,
>                        body=message)
> print " [x] Sent %r:%r" % (routing_key, message)
> connection.close()
>
>
>
> and receive_1.py
> ============================================
> #!/usr/bin/env python
> import pika
> import sys
>
> connection = pika.BlockingConnection(pika.ConnectionParameters(
>          host='localhost'))
> channel = connection.channel()
>
> channel.exchange_declare(exchange='workflow',
>                           type='topic')
>
> result = channel.queue_declare(queue='workflow')
> queue_name = result.method.queue
>
> binding_keys = sys.argv[1:]
> if not binding_keys:
>      print >> sys.stderr, "Usage: %s [binding_key]..." % (sys.argv[0],)
>      sys.exit(1)
>
> for binding_key in binding_keys:
>      print "binding_key ",binding_key
>      channel.queue_bind(exchange='workflow',
>                         queue=queue_name,
>                         routing_key=binding_key)
>
> print ' [*] Waiting for logs. To exit press CTRL+C'
>
> def callback(ch, method, properties, body):
>      print " [x] %r:%r" % (method.routing_key, body,)
>
> channel.basic_consume(callback,
>                        queue=queue_name,
>                        no_ack=True)
>
> channel.start_consuming()
>
>
> _______________________________________________
> rabbitmq-discuss mailing list
> rabbitmq-discuss at lists.rabbitmq.com
> https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss


-- 
Simon MacMullen
RabbitMQ, Pivotal


More information about the rabbitmq-discuss mailing list