rabbitMQ实战(一)---------使用pika库实现hello world
来源:互联网 发布:淘宝产品拍摄布光图 编辑:程序博客网 时间:2024/04/28 14:42
pika是RabbitMQ团队编写的官方Python AMQP库。
需要先安装pika:
pip3 install pika
有较详细的注释,就不再详细说明了
生产者代码:
hello_world_producer.py:
import pika,sys#connect to the rabbitmq,use the default vhostcredentials = pika.PlainCredentials("guest","guest")conn_params = pika.ConnectionParameters("localhost", credentials=credentials)conn_broker = pika.BlockingConnection(conn_params)#get a channel used to communicate with the rabbitmqchannel = conn_broker.channel()#declare a exchangechannel.exchange_declare(exchange='hello-exchange', type='direct', passive=False, #if the exchange already existes,report a error.It means we want to declare an exchange. durable=True, #durable the message auto_delete=False) #if the last consumer is over,do not delete the exchange auto#create a messagemsg = sys.argv[1]msg_props = pika.BasicProperties()msg_props.content_type = "text/plain"#publish the messagechannel.basic_publish(body=msg, exchange='hello-exchange', properties=msg_props, routing_key='hola')
消费者代码
hello_world_consumer.py:
import pika#connect to the rabbitmq,use the default vhostcredentials = pika.PlainCredentials("guest","guest")conn_params = pika.ConnectionParameters("localhost", credentials=credentials)conn_broker = pika.BlockingConnection(conn_params)#get a channel used to communicate with the rabbitmqchannel = conn_broker.channel()#declare a exchangechannel.exchange_declare(exchange='hello-exchange', type='direct', passive=False, #if the exchange already existes,report a error.It means we want to declare an exchange. durable=True, #durable the message auto_delete=False) #if the last consumer is over,do not delete the exchange auto#declare a queuechannel.queue_declare(queue="hello-queue")#bind queue to an exchangechannel.queue_bind(queue='hello-queue', exchange='hello-exchange', routing_key='hola')#define the consumer method to consumer message from a queuedef msg_consumer(channel,method,header,body): channel.basic_ack(delivery_tag=method.delivery_tag) if body.decode("ascii") == "quit": channel.basic_cancel(consumer_tag='hello-consumer') channel.stop_consuming() else: print(body) return#subscribe messagechannel.basic_consume(msg_consumer, queue='hello-queue', consumer_tag='hello-consumer')#begin loop until a quit message is sentchannel.start_consuming()
运行代码:
需要先运行consumer,因为我们是在消费者中创建队列的,如果先生产消息,由于没有可以路由到的队列,消息会被丢弃。
$ python hello_world_consumer.pyb'good'b'hello world'
$ python hello_world_producer.py "good"$ python hello_world_producer.py "hello world"$ python hello_world_producer.py "quit"
0 0
- rabbitMQ实战(一)---------使用pika库实现hello world
- rabbitmq(一)Hello world
- RabbitMQ实现‘Hello world’
- rabbitmq 教程 一 hello world
- RabbitMQ学习一:hello world
- 在Node.js中使用RabbitMQ系列一 Hello world
- Python下使用pika简单实现RabbitMQ通信
- RabbitMQ消息队列之“Hello World”(一)
- RabbitMQ (一)第一个hello world
- RabbitMQ学习小结(一)----hello world
- java版RabbitMQ的hello world实现
- 02-RabbitMQ Hello World Spring使用
- RabbitMQ之"Hello World!"
- RabbitMQ -- Hello world
- RabbitMQ Hello World
- rabbitMq之:Hello World!
- “Hello world” of RabbitMQ
- “Hello world” of RabbitMQ
- C语言可以给字符数组赋值的方法
- java垃圾回收算法之-引用计数器
- 7月编程语言排行榜
- 规范unity脚本书写
- 堆排序
- rabbitMQ实战(一)---------使用pika库实现hello world
- 四季如何养脾胃
- 人体穴位按摩治病法
- 第一篇博客 试试水
- wamp安装Zend Optimizer
- sqlite至mysql的转换
- Java for Android
- Fatal error: Allowed memory size of 8388608 bytes exhausted問題解決方式
- IE6,IE7,FF | CSS + DIV 兼容问题综合解决方案