python rabbitmq官方文档demo
1.生产者
代码语言:javascript代码运行次数:0运行复制#!/usr/bin/env python
import pika
import json
# .html 官方文档
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
message=json.dumps({'OrderId':"1000"})
channel.basic_publish(exchange='',
routing_key='hello',
body=message)
#print(" [x] Sent 'Hello World! 2020'")
print(message)
connection.close()
2.消费者
代码语言:javascript代码运行次数:0运行复制#!/usr/bin/env python
import pika, sys, os
# .html 官方文档
def main():
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
def callback(ch, method, properties, body):
#print(" [x] Received [%r]" %body)
#print(body) # b'{"OrderId": "1000"}'
print(body.decode()) ## 关键:需要decode,否则会出现上面的b' '符号
channel.basic_consume(queue='hello',
auto_ack=True,
on_message_callback=callback)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print('Interrupted')
try:
sys.exit(0)
except SystemExit:
os._exit(0)
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2020-11-25,如有侵权请联系 cloudcommunity@tencent 删除pythonrabbitmqchannel官方文档queue发布者:admin,转转请注明出处:http://www.yc00.com/web/1754991121a5224642.html
评论列表(0条)