使用rabbitmq消息队列
2018-06-18 00:07:56来源:未知 阅读 ()
一、前言
在python中本身就是存在队列queue。一个是线程队列queue,另一个是进程multiprocessing中的队列Queue。
线程queue:只用于线程之间的数据交互
进程Queue:用于同一进程下父进程和子进程之间的数据交互,或者同属于一个父进程下的多个子进程之间的交互
二、RabbitMQ
如果是多个进程间(不同的应用程序之间)、多个系统需要进行数据交互,那么就可以使用消息服务来解决这些问题。消息服务擅长于解决多系统、异构系统间的数据交换(消息通知/通讯)问题,你也可以把它用于系统间服务的相互调用(RPC)。RabbitMQ就是当前最主流的消息中间件之一。
RabbitMQ的基础介绍:点击查看
实现最简单的队列通信

send端
# -*- coding: UTF-8 -*-
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
'localhost'))
# 声明一个管道
channel = connection.channel()
# 声明queue
channel.queue_declare(queue='hello') # queue的名称
# n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close()
receive端
# -*- coding: UTF-8 -*-
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
'localhost'))
channel = connection.channel()
# You may ask why we declare the queue again ? we have already declared it in our previous code.
# We could avoid that if we were sure that the queue already exists. For example if send.py program
# was run before. But we're not yet sure which program to run first. In such cases it's a good
# practice to repeat declaring the queue in both programs.
# 这个queue名称就是刚才send端中声明的,再次声明就能保证找到生产者发送的数据
# 如果消费者在生产者之前先启动了,会找不到这个消息队列,就会报错
channel.queue_declare(queue='hello')
def callback(ch, method, properties, body):
print('--->>', ch, '\n', method, '\n', properties)
print(" [x] Received %r" % body)
# ch: 声明的管道channel对象内存地址
#
channel.basic_consume(callback, # 如果收到消息就调用callback函数来处理消息
queue='hello',
no_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
# 永远收下去,没有就在这卡住
channel.start_consuming()
三、连接远程rabbitmq
连接远程rabbitmq可能会有认证问题,需要输入用户名和密码
windows下设置用户名、密码和权限:here
linux环境下设置用户名、密码和权限:here
基本都在最后面
send端的设置:
# -*- conding:utf-8 -*-
import pika
# 认证信息
credentials = pika.PlainCredentials('bigberg', '111111')
connection = pika.BlockingConnection(pika.ConnectionParameters(
'172.16.200.109', 5672, '/', credentials))
# 声明一个管道
channel = connection.channel()
# 声明queue
channel.queue_declare(queue='hello') # queue的名称
# n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
channel.basic_publish(exchange='',
routing_key='hello',
body='from remote server')
print(" [x] Sent 'from remote server'")
connection.close()
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:并发服务器几种实现方法总结
下一篇:019sys模块
- Django项目中使用qq第三方登录。 2019-08-13
- Python连载30-多线程之进程&线程&线程使用 2019-08-13
- Python学习日记(十) 生成器和迭代器 2019-08-13
- 【Python】语法基础 | 开始使用Python 2019-08-13
- 使用scrapy框架爬取全书网书籍信息。 2019-08-13
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
