python rabbitmq操作
一个队列有多个消费者的情况下,每个消费者会拿到相对平均的消息;如果想让每个消费者可见全部的消息,那么每个消费者绑定同一个路由,而指定不同的队列,可以用随机队列解决。
send.py:
import pika
def send_message_withoutexchange():
username = 'wj' # 指定远程rabbitmq的用户名密码
pwd = '123456a?'
user_pwd = pika.PlainCredentials(username, pwd)
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='127.0.0.1', port=5672, credentials=user_pwd)) # 定义连接池
channel = connection.channel() # 声明队列以向其发送消息消息
channel.queue_declare(queue='test_persistent', durable=True)
for i in range(100000):
channel.basic_publish(exchange='', routing_key='test_persistent', body=str(i),
properties=pika.BasicProperties(delivery_mode=2))
print('send success msg[%s] to rabbitmq' % i)
connection.close() # 关闭连接
def send_message_direct():
username = 'wj' # 指定远程rabbitmq的用户名密码
pwd = '123456a?'
user_pwd = pika.PlainCredentials(username, pwd)
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='127.0.0.1', port=5672, credentials=user_pwd)) # 定义连接池
channel = connection.channel() # 声明队列以向其发送消息消息
# channel.queue_declare(queue='test_persistent', durable=True)
channel.exchange_declare(exchange='direct_logs',
exchange_type='direct',durable=True) # 创建一个交换机并声明exchange的类型为:关键字类型,表示该交换机会根据消息中不同的关键字将消息发送给不同的队列
severity = 'info' # severity这里只能为一个字符串,这里为‘info’表明本生产者只将下面的message发送到info队列中,消费者也只能从info队列中接收info消息
message = 'Hello World!{}'
for i in range(100000):
channel.basic_publish(exchange='direct_logs', # 指明用于发布消息的交换机、关键字
routing_key=severity, # 绑定关键字,即将message与关键字info绑定,明确将消息发送到哪个关键字的队列中。
body=message.format(i))
print(" [生产者] Sent %r:%r" % (severity, message))
connection.close()
send_message_direct()
receive.py:
import pika
import time
def receive_message_withoutexchange():
username = 'wj' # 指定远程rabbitmq的用户名密码
pwd = '123456a?'
user_pwd = pika.PlainCredentials(username, pwd)
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='127.0.0.1', port=5672, credentials=user_pwd))
channel = connection.channel()
channel.queue_declare(queue='test_persistent', durable=True)
def callback(ch, method, properties, body):
'''回调函数,处理从rabbitmq中取出的消息'''
print(" [x] Received %r" % body)
# time.sleep(5)
ch.basic_ack(delivery_tag=method.delivery_tag) # 发送ack消息
channel.basic_qos(prefetch_count=1)
channel.basic_consume(callback, queue='test_persistent', no_ack=False)
print(' Waiting for messages')
channel.start_consuming() # 开始监听 接受消息
def receive_message_direct():
username = 'wj' # 指定远程rabbitmq的用户名密码
pwd = '123456a?'
user_pwd = pika.PlainCredentials(username, pwd)
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='127.0.0.1', port=5672, credentials=user_pwd))
channel = connection.channel()
channel.exchange_declare(exchange='direct_logs',
exchange_type='direct', durable=True) # 创建交换机,命名为‘direct_logs’并声明exchange类型为关键字类型。
result = channel.queue_declare(exclusive=True, durable=True) # 创建随机一个队列当消费者退出的时候,该队列被删除。
queue_name = result.method.queue # 创建一个随机队列名字。
if not queue_name:
# 如果无法使用随机队列则使用声明的方式创建队列
queue_name = "test_persistent"
channel.queue_declare(queue='test_persistent', durable=True)
channel.queue_bind(exchange='direct_logs', # 将交换机、队列、关键字绑定在一起,使消费者只能根据关键字从不同队列中取消息
queue=queue_name,
routing_key="info") # 该消费者绑定的关键字。
def callback(ch, method, properties, body):
'''回调函数,处理从rabbitmq中取出的消息'''
print(" [x] Received %r" % body)
# time.sleep(5)
ch.basic_ack(delivery_tag=method.delivery_tag) # 发送ack消息
channel.basic_qos(prefetch_count=1)
channel.basic_consume(callback, queue=queue_name, no_ack=False)
print(' Waiting for messages')
channel.start_consuming() # 开始监听 接受消息
receive_message_direct()
浙公网安备 33010602011771号