好三网网站,wordpress用户管理插件,提供网站建设公,广东做陶瓷的网站介绍
RabbitMQ 是一个消息中间件#xff0c;它实现了 AMQP (Advanced Message Queuing Protocol) 协议。本教程将引导你通过几个简单的步骤来学习如何使用 RabbitMQ 发送和接收消息。
环境准备
1. 安装 RabbitMQ
- 在你的系统上安装 RabbitMQ: https://www.rabbitmq.com/d…
介绍
RabbitMQ 是一个消息中间件它实现了 AMQP (Advanced Message Queuing Protocol) 协议。本教程将引导你通过几个简单的步骤来学习如何使用 RabbitMQ 发送和接收消息。
环境准备
1. 安装 RabbitMQ
- 在你的系统上安装 RabbitMQ: https://www.rabbitmq.com/download.html
- 启动服务: sudo rabbitmq-server
2. 安装客户端库
- Python 示例将使用 pika 库: pip install pika
第一步: 创建生产者
创建一个简单的生产者用于发送消息到 RabbitMQ 服务器。
python
import pika
def main():
connection pika.BlockingConnection(pika.ConnectionParameters(localhost))
channel connection.channel()
channel.queue_declare(queuehello)
message Hello World!
channel.basic_publish(exchange,
routing_keyhello,
bodymessage)
print( [x] Sent %r % message)
connection.close()
if __name__ __main__:
main() 第二步: 创建消费者
创建一个简单的消费者用于接收来自 RabbitMQ 服务器的消息。
python
import pika
def callback(ch, method, properties, body):
print( [x] Received %r % body)
def main():
connection pika.BlockingConnection(pika.ConnectionParameters(localhost))
channel connection.channel()
channel.queue_declare(queuehello)
channel.basic_consume(queuehello,
on_message_callbackcallback,
auto_ackTrue)
print( [*] Waiting for messages. To exit press CTRLC)
channel.start_consuming()
if __name__ __main__:
main() 第三步: 使用持久化消息
确保消息在 RabbitMQ 重启后仍然存在。
生产者代码修改
python
import pika
def main():
connection pika.BlockingConnection(pika.ConnectionParameters(localhost))
channel connection.channel()
channel.queue_declare(queuehello, durableTrue)
message Hello World!
channel.basic_publish(exchange,
routing_keyhello,
bodymessage,
propertiespika.BasicProperties(
delivery_mode2, # make message persistent
))
print( [x] Sent %r % message)
connection.close()
if __name__ __main__:
main() 消费者代码修改
python
import pika
def callback(ch, method, properties, body):
print( [x] Received %r % body)
def main():
connection pika.BlockingConnection(pika.ConnectionParameters(localhost))
channel connection.channel()
channel.queue_declare(queuehello, durableTrue)
channel.basic_qos(prefetch_count1)
channel.basic_consume(queuehello,
on_message_callbackcallback,
auto_ackFalse)
print( [*] Waiting for messages. To exit press CTRLC)
channel.start_consuming()
if __name__ __main__:
main() 第四步: 使用工作队列
实现一个简单的工作队列可以分发任务给多个工作者。
生产者
python
import pika
import sys
import random
def main():
connection pika.BlockingConnection(pika.ConnectionParameters(localhost))
channel connection.channel()
channel.queue_declare(queuetask_queue, durableTrue)
message .join(sys.argv[1:]) or Hello World!
message f {random.randint(1, 10)}
channel.basic_publish(
exchange,
routing_keytask_queue,
bodymessage,
propertiespika.BasicProperties(delivery_mode2)) # make message persistent
print( [x] Sent %r % message)
connection.close()
if __name__ __main__:
main() 工作者
python
import pika
import time
def callback(ch, method, properties, body):
print( [x] Received %r % body)
time.sleep(body.count(b.))
print( [x] Done)
ch.basic_ack(delivery_tagmethod.delivery_tag)
def main():
connection pika.BlockingConnection(pika.ConnectionParameters(localhost))
channel connection.channel()
channel.queue_declare(queuetask_queue, durableTrue)
print( [*] Waiting for messages. To exit press CTRLC)
channel.basic_qos(prefetch_count1)
channel.basic_consume(queuetask_queue,
on_message_callbackcallback)
channel.start_consuming()
if __name__ __main__:
main()