Python编程:RabbitMQ消息队列

简介: Python编程:RabbitMQ消息队列

不同程序之间通讯

1.socket

2.disk硬盘文件

3.broker中间代理


python中:

threading Queue 线程之间通讯,不能跨进程

multiprocessing Queue 父进程与子进程进行交互,或同一个父进程下的多个子进程


RabbitMQ 消息队列

MQ全称为Message Queue,一种应用程序对应用程序的通信方法


RabbitMQ 官方地址:http://www.rabbitmq.com/


安装:

erlang http://www.erlang.org/

rabbitmq http://www.rabbitmq.com/


参考:

《Windows下RabbitMQ安装及入门》

http://blog.csdn.net/hzw19920329/article/details/53156015


启动服务(管理员模式):rabbitmq-service start 或者:services.msc


访问管理后台:

http://localhost:15672 用户名:guest,密码:guest


查看队列:rabbitmqctl list_queues


查看状态:rabbitmqctl status


rabbitMQ轮询分发

将消息依次分发给每个消费者


生产者 –> 队列 –> 消费者1, 消费者2, 消费者3


rabbitMQ消息持久化

服务器关闭,队列消失,可以设置持久化队列名,持久化消息


消费者控制接收数量

消费者可以按需获取信息,实现能者多劳


发布者订阅者

是即时发送接收


发布者 –> 交换机 –> 队列1, 队列2, 队列3 –> 订阅者1, 订阅者2, 订阅者3


广播模式

fanout广播模式 无选择接收

direct广播模式 有选择接收 队列绑定关键字

topic广播模式 消息过滤


遇到的问题

问题:无法访问Web管理页面

解决:

启动管理模块:

rabbitmqctl start_app

rabbitmq-plugins enable rabbitmq_management

rabbitmqctl stop


参考:

《RabbitMQ无法访问Web管理页面》

http://blog.csdn.net/u011642663/article/details/54691788


问题:Error: unable to perform an operation

解决:

C:\Windows\System32\config\systemprofile.erlang.cookie

拷贝.erlang.cookie文件覆盖

C:\User\username.erlang.cookie


参考:

《Authentication failed》

http://blog.csdn.net/j_shine/article/details/78833456


代码实例

安装第三方库:

pip install pika

生产者消费者

# 发送端
import pika  
queue_name = "hello2" # 队列名称
connection = pika.BlockingConnection(
    pika.ConnectionParameters("localhost")
)
channel = connection.channel()
# 声明queue
channel.queue_declare(queue=queue_name)  # durable=True消息名称持久化
# 发送消息,需要通过路由器
channel.basic_publish(exchange="",
                      routing_key=queue_name,
                      body="hello, world!",
                      # 消息持久化 make message persistent
                      #properties=pika.BasicProperties(delivery_mode=2)
                      )
print("send hello")
connection.close()
# 持久化之后,服务器重启不消失


# 接收端
import pika
import time
queue_name = "hello2" # 队列名称
connection = pika.BlockingConnection(
    pika.ConnectionParameters("localhost")
)
channel = connection.channel()
# 不知道客户端还是服务端先启动,为了确保这个队列存在,两端都需要声明
channel.queue_declare(queue=queue_name)
channel.basic_qos(prefetch_count=1)  # 最多处理一个信息,处理完再接收
def callback(ch, method, properties, body): # 回调函数
    print("ch:", ch)
    print("method:", method)
    print("properties:", properties)
    print("接收到信息:", body)
    time.sleep(30)
    # ch.basic_ask(delivery_tag=method.delivery_tag)  # 消息确认
channel.basic_consume(callback,  # 如果收到消息就调用函数处理消息
                      queue=queue_name,
                      no_ack=True)  #acknowledgement确认
print("waiting for message, ctrl+c break")
# 启动接收
channel.start_consuming()

1对多的发送广播

广播模式:fanout


# 发布者
import pika
connection = pika.BlockingConnection(
    pika.ConnectionParameters("localhost")
)
channel = connection.channel()
channel.exchange_declare(exchange="logs", exchange_type="fanout")
message = "hello world"
channel.basic_publish(exchange="logs",
                      routing_key="",
                      body=message)
print("send ok")
connection.close()
# 订阅者
import pika
connection = pika.BlockingConnection(
    pika.ConnectionParameters("localhost")
)
channel = connection.channel()
channel.exchange_declare(exchange="logs", exchange_type="fanout")
result = channel.queue_declare(exclusive=True)  # 随机分配队列名
queue_name = result.method.queue
channel.queue_bind(exchange="logs", queue=queue_name)
print("waiting for logs")
def callback(ch, method, properties, body):
    print("body:", body)
channel.basic_consume(callback, queue=queue_name, no_ack=True)
channel.start_consuming()

消息过滤

有选择的接收消息,广播模式:direct


# 实现1对多的发送


# 发布者
import pika
import sys
connection = pika.BlockingConnection(
    pika.ConnectionParameters("localhost")
)
channel = connection.channel()
channel.exchange_declare(exchange="direct_logs", exchange_type="direct")
severity = sys.argv[1] if len(sys.argv)>1 else "info"  # severity严重程度
message = " ".join(sys.argv[:2] or "hello world!")
channel.basic_publish(exchange="direct_logs",
                      routing_key=severity,
                      body=message)
print("send ok")
connection.close()

# 订阅者



import pika
import sys
connection = pika.BlockingConnection(
    pika.ConnectionParameters("localhost")
)
channel = connection.channel()
channel.exchange_declare(exchange="direct_logs", exchange_type="direct")
result = channel.queue_declare(exclusive=True)  # 随机分配队列名
queue_name = result.method.queue
# 获取参数
severities = sys.argv[1:]
if not severities:
    sys.stderr.write("usage: %s [info] [warning] [error]" % sys.argv[0])
    sys.exit(1)
for severity in severities:
    channel.queue_bind(exchange="direct_logs",
                       routing_key=severity,
                       queue=queue_name)
print("waiting...")
def callback(ch, method, properties, body):
    print("routing_key:", method.routing_key)
    print("body:", body)
channel.basic_consume(callback, queue=queue_name, no_ack=True)
channel.start_consuming()

通配符消息过滤

表达式符号说明:#代表一个或多个字符,*代表任何字符

广播模式:topic


# 实现1对多的发送


# 发布者
import pika
import sys
connection = pika.BlockingConnection(
    pika.ConnectionParameters("localhost")
)
channel = connection.channel()
channel.exchange_declare(exchange="topic_logs", exchange_type="topic")
severity = sys.argv[1] if len(sys.argv)>1 else "info"  # severity严重程度
message = " ".join(sys.argv[:2] or "hello world!")
channel.basic_publish(exchange="topic_logs",
                      routing_key=severity,
                      body=message)
print("send ok")
connection.close()


# 订阅者
import pika
import sys
connection = pika.BlockingConnection(
    pika.ConnectionParameters("localhost")
)
channel = connection.channel()
channel.exchange_declare(exchange="topic_logs", exchange_type="topic")
result = channel.queue_declare(exclusive=True)  # 随机分配队列名
queue_name = result.method.queue
# 获取参数
severities = sys.argv[1:]
if not severities:
    sys.stderr.write("usage: %s [info] [warning] [error]" % sys.argv[0])
    sys.exit(1)
for severity in severities:
    channel.queue_bind(exchange="topic_logs",
                       routing_key=severity,
                       queue=queue_name)
print("waiting...")
def callback(ch, method, properties, body):
    print("routing_key:", method.routing_key)
    print("body:", body)
channel.basic_consume(callback, queue=queue_name, no_ack=True)
channel.start_consuming()

Remote procedure call (RPC)

远程程序调用



# 服务器端
import pika
import time
connection = pika.BlockingConnection(
    pika.ConnectionParameters(host='localhost')
)
channel = connection.channel()
channel.queue_declare(queue='rpc_queue')
def fib(n):  # 被调用函数
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fib(n - 1) + fib(n - 2)
def on_request(ch, method, props, body):
    n = int(body)
    print(" [.] fib(%s)" % n)
    response = fib(n)
    ch.basic_publish(exchange='',
                     routing_key=props.reply_to,
                     properties=pika.BasicProperties(
                         correlation_id= props.correlation_id
                     ),
                     body=str(response))
    ch.basic_ack(delivery_tag=method.delivery_tag)
channel.basic_qos(prefetch_count=1)
channel.basic_consume(on_request, queue='rpc_queue')
print(" [x] Awaiting RPC requests")
channel.start_consuming()


# 客户端
import pika
import uuid
class FibonacciRpcClient(object):
    def __init__(self):
        self.connection = pika.BlockingConnection(
            pika.ConnectionParameters(host='localhost')
        )
        self.channel = self.connection.channel()
        # 生成随机队列
        result = self.channel.queue_declare(exclusive=True)
        self.callback_queue = result.method.queue
        self.channel.basic_consume(self.on_response,
                                   no_ack=True,
                                   queue=self.callback_queue
                                   )
    def on_response(self, ch, method, props, body):
        if self.corr_id == props.correlation_id:
            self.response = body
    def call(self, n):
        self.response = None
        self.corr_id = str(uuid.uuid4())  # 用于区分发送指令和接收结果的一致性
        self.channel.basic_publish(exchange='',
                                   routing_key='rpc_queue',
                                   properties=pika.BasicProperties(
                                       reply_to=self.callback_queue,
                                       correlation_id=self.corr_id,
                                   ),
                                   body=str(n))
        while self.response is None:
            self.connection.process_data_events()
        return int(self.response)
fibonacci_rpc = FibonacciRpcClient()
print(" [x] Requesting fib(30)")
response = fibonacci_rpc.call(30)
print(" [.] Got %r" % response)

参考文章:

《Python之路,Day9 - 异步IO\数据库\队列\缓存》

http://www.cnblogs.com/alex3714/articles/5248247.html


相关实践学习
RocketMQ一站式入门使用
从源码编译、部署broker、部署namesrv,使用java客户端首发消息等一站式入门RocketMQ。
消息队列 MNS 入门课程
1、消息队列MNS简介 本节课介绍消息队列的MNS的基础概念 2、消息队列MNS特性 本节课介绍消息队列的MNS的主要特性 3、MNS的最佳实践及场景应用 本节课介绍消息队列的MNS的最佳实践及场景应用案例 4、手把手系列:消息队列MNS实操讲 本节课介绍消息队列的MNS的实际操作演示 5、动手实验:基于MNS,0基础轻松构建 Web Client 本节课带您一起基于MNS,0基础轻松构建 Web Client
相关文章
|
15天前
|
安全 Java 数据处理
Python网络编程基础(Socket编程)多线程/多进程服务器编程
【4月更文挑战第11天】在网络编程中,随着客户端数量的增加,服务器的处理能力成为了一个重要的考量因素。为了处理多个客户端的并发请求,我们通常需要采用多线程或多进程的方式。在本章中,我们将探讨多线程/多进程服务器编程的概念,并通过一个多线程服务器的示例来演示其实现。
|
15天前
|
程序员 开发者 Python
Python网络编程基础(Socket编程) 错误处理和异常处理的最佳实践
【4月更文挑战第11天】在网络编程中,错误处理和异常管理不仅是为了程序的健壮性,也是为了提供清晰的用户反馈以及优雅的故障恢复。在前面的章节中,我们讨论了如何使用`try-except`语句来处理网络错误。现在,我们将深入探讨错误处理和异常处理的最佳实践。
|
18天前
|
缓存 监控 Python
解密Python中的装饰器:优雅而强大的编程利器
Python中的装饰器是一种强大而又优雅的编程工具,它能够在不改变原有代码结构的情况下,为函数或类添加新的功能和行为。本文将深入解析Python装饰器的原理、用法和实际应用,帮助读者更好地理解和利用这一技术,提升代码的可维护性和可扩展性。
|
18小时前
|
机器学习/深度学习 人工智能 数据可视化
Python:探索编程之美
Python:探索编程之美
8 0
|
23小时前
|
机器学习/深度学习 人工智能 数据处理
Python编程的魅力与实践
Python编程的魅力与实践
|
1天前
|
SQL 关系型数据库 MySQL
第十三章 Python数据库编程
第十三章 Python数据库编程
|
2天前
|
存储 网络协议 关系型数据库
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信
|
7天前
|
安全 数据处理 开发者
《Python 简易速速上手小册》第7章:高级 Python 编程(2024 最新版)
《Python 简易速速上手小册》第7章:高级 Python 编程(2024 最新版)
19 1
|
7天前
|
人工智能 数据挖掘 程序员
《Python 简易速速上手小册》第1章:Python 编程入门(2024 最新版)
《Python 简易速速上手小册》第1章:Python 编程入门(2024 最新版)
35 0
|
8天前
|
API Python
Python模块化编程:面试题深度解析
【4月更文挑战第14天】了解Python模块化编程对于构建大型项目至关重要,它涉及代码组织、复用和维护。本文深入探讨了模块、包、导入机制、命名空间和作用域等基础概念,并列举了面试中常见的模块导入混乱、不适当星号导入等问题,强调了避免循环依赖、合理使用`__init__.py`以及理解模块作用域的重要性。掌握这些知识将有助于在面试中自信应对模块化编程的相关挑战。
21 0

相关产品

  • 云消息队列 MQ