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

Darren Govoni darren at ontrenet.com
Wed Sep 4 13:57:54 BST 2013


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()




More information about the rabbitmq-discuss mailing list