和第一篇里的receive.py相比,主要是做了两个改动:
定义交换机
不使用hello队列了,随机生成一个临时队列,并绑定到交换机上
#!/usr/bin/env python #coding=utf8 import pika connection= pika.BlockingConnection(pika.ConnectionParameters( 'localhost')) channel= connection.channel() #定义交换机 channel.exchange_declare(exchange='messages',type='fanout') #随机生成队列,并绑定到交换机上 result= channel.queue_declare(exclusive=True) queue_name= result.method.queue channel.queue_bind(exchange='messages', queue=queue_name) def callback(ch, method, properties, body): print " [x] Received %r" % (body,) channel.basic_consume(callback, queue=queue_name, no_ack=True) print ' [*] Waiting for messages. To exit press CTRL+C' channel.start_consuming()
和第一篇的send.py相比,也只做了两个改动:
定义交换机
不是将消息发送到hello队列,而是发送到交换机
#!/usr/bin/env python #coding=utf8 import pika connection= pika.BlockingConnection(pika.ConnectionParameters( 'localhost')) channel= connection.channel() #定义交换机 channel.exchange_declare(exchange='messages',type='fanout') #将消息发送到交换机 channel.basic_publish(exchange='messages', routing_key='', body='Hello World!') print " [x] Sent 'Hello World!'" connection.close()
打开另外一个终端,执行send.py,可以观察到receive.py接收到了消息。如果有多个终端执行receive.py,那么每个receive.py都会接收到消息。
以上就是Python rabbitmq的使用(三)的内容,更多相关内容请关注PHP中文网(HdhCmsTestgxlcms测试数据)!
查看更多关于Pythonrabbitmq的使用(三)的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did82859