1.1 生产者
-
producer.py
import pika # 1 链接rabbitmq connection = pika.BlockingConnection(pika.ConnectionParameters('127.0.0.1')) channel = connection.channel() # 2 创建队列 channel.queue_declare(queue='queue_test1') # 3 向指定队列插入数据 channel.basic_publish(exchange='', # 简单模式 routing_key='queue_test1', # 指定队列 body=b'this is test1!') print("send ok!") # 4 断开链接 connection.close()
1.2 消费者
-
customer.py
import pika connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) channel = connection.channel() # 创建队列 channel.queue_declare(queue='queue_test1') # 确定回调函数 def callback(ch, method, properties, body): pass print("callback recv: %r" % body) # 确定监听队列参数 channel.basic_consume(queue='queue_test1', auto_ack=True, # 默认应答 on_message_callback=callback) print('ready ok!') # 正式监听 channel.start_consuming()