# 环境为CentOS 7 yum install rabbitmq-server # 安装RabbitMQ systemctl start rabbitmq-server # 启动 systemctl enable rabbitmq-server # 开机自启 systemctl stop firewall-cmd # 临时关闭防火墙
#!/usr/bin/env python3 # coding=utf-8 # @Time : 2017/6/13 19:25 # @Author : Shawn # @Blog : https://blog.just666.cn # @Email : shawnbluce@gmail测试数据 # @purpose : RabbitMQ_Producer import pika # 创建连接对象 connection = pika.BlockingConnection(pika.ConnectionParameters(host='115.xx.xx.xx')) # 创建频道对象 channel = connection.channel() # 指定一个队列,如果该队列不存在则创建 channel.queue_declare(queue='test_queue') # 提交消息 for i in range(10): channel.basic_publish(exchange='', routing_key='test_queue', body='hello,world' + str(i)) print("sent...") # 关闭连接 connection.close()
#!/usr/bin/env python3 # coding=utf-8 # @Time : 2017/6/13 19:40 # @Author : Shawn # @Blog : https://blog.just666.cn # @Email : shawnbluce@gmail测试数据 # @purpose : RabbitMQ_Consumer import pika credentials = pika.PlainCredentials('guest', 'guest') # 连接到RabbitMQ服务器 connection = pika.BlockingConnection(pika.ConnectionParameters('115.xx.xx.xx', 5672, '/', credentials)) channel = connection.channel() # 指定一个队列,如果该队列不存在则创建 channel.queue_declare(queue='test_queue') # 定义一个回调函数 def callback(ch, method, properties, body): print(body.decode('utf-8')) # 告诉RabbitMQ使用callback来接收信息 channel.basic_consume(callback, queue='test_queue', no_ack=False) print('waiting...') # 开始接收信息,并进入阻塞状态,队列里有信息才会调用callback进行处理。按ctrl+c退出。 channel.start_consuming()
#!/usr/bin/env python3 # coding=utf-8 # @Time : 2017/6/13 20:21 # @Author : Shawn # @Blog : https://blog.just666.cn # @Email : shawnbluce@gmail测试数据 # @purpose : RabbitMQ_Publisher import pika # 创建连接对象 connection = pika.BlockingConnection(pika.ConnectionParameters(host='115.xx.xx.xx')) # 创建频道对象 channel = connection.channel() # 定义交换机,exchange表示交换机名称,type表示类型 channel.exchange_declare(exchange='my_fanout', type='fanout') message = 'Hello Python' # 将消息发送到交换机 channel.basic_publish(exchange='my_fanout', # 指定exchange routing_key='', # fanout下不需要配置,配置了也不会生效 body=message) connection.close()
#!/usr/bin/env python3 # coding=utf-8 # @Time : 2017/6/13 20:20 # @Author : Shawn # @Blog : https://blog.just666.cn # @Email : shawnbluce@gmail测试数据 # @purpose : RabbitMQ_Subscriber import pika credentials = pika.PlainCredentials('guest', 'guest') # 连接到RabbitMQ connection = pika.BlockingConnection(pika.ConnectionParameters('115.xx.xx.xx', 5672, '/', credentials)) channel = connection.channel() # 定义交换机,进行exchange声明,exchange表示交换机名称,type表示类型 channel.exchange_declare(exchange='my_fanout', type='fanout') # 随机创建队列 result = channel.queue_declare(exclusive=True) # exclusive=True表示建立临时队列,当consumer关闭后,该队列就会被删除 queue_name = result.method.queue # 将队列与exchange进行绑定 channel.queue_bind(exchange='my_fanout', queue=queue_name) # 定义回调方法 def callback(ch, method, properties, body): print(body.decode('utf-8')) # 从队列获取信息 channel.basic_consume(callback, queue=queue_name, no_ack=True) channel.start_consuming()
总结
以上就是关于Python如何操作消息队列(RabbitMQ)的方法教程的详细内容,更多请关注Gxl网其它相关文章!
查看更多关于关于Python如何操作消息队列(RabbitMQ)的方法教程的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did84668