python 操作消息队列
图示

其中P指producer,即生产者;C指consumer,即消费者。中间的红色表示消息队列,实例中表现为HELLO队列。
往队列里插入数据前,查看消息队列
$sudo rabbitmqctl list_queues
Listing queues ...
celeryev.db53a5e0-1e6a-4f06-a9f7-2c104c4612fb
...done.
插入消息队列代码
#in_queue.py
#coding=utf8
import pika connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel() #声明队列,如果消息发送到不存在的队列,rabbitmq会自动清除这些消息
channel.queue_declare(queue='HELLO') for i in range(10):
#exchange表示交换器,可以精确的制定消息应发到哪个队列,route_key设置队列的名称,body表示发送的内容
channel.basic_publish(exchange='', routing_key='HELLO', body='Hello World!' + str(i))
print " [%d] Sent 'Hello World!'" % i
#关闭连接
connection.close()
执行结果
$python in_queue.py
[0] Sent 'Hello World!'
[1] Sent 'Hello World!'
[2] Sent 'Hello World!'
[3] Sent 'Hello World!'
[4] Sent 'Hello World!'
[5] Sent 'Hello World!'
[6] Sent 'Hello World!'
[7] Sent 'Hello World!'
[8] Sent 'Hello World!'
[9] Sent 'Hello World!'
此时查看消息队列
$sudo rabbitmqctl list_queues
Listing queues ...
HELLO 10
celeryev.db53a5e0-1e6a-4f06-a9f7-2c104c4612fb 0
...done.
可以看到队列HELLO里面有10条数据。
读取消息队列数据
#out_queue.py
#coding=utf8
import pika connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel() channel.queue_declare(queue='HELLO') def callback(ch, method, properties, body):
print " [x] Received %r" % (body,) channel.basic_consume(callback, queue='HELLO', no_ack=True) print ' [*] Waiting for messages. To exit press CTRL+C'
channel.start_consuming()
执行结果
$python out_queue.py
[*] Waiting for messages. To exit press CTRL+C
[x] Received 'Hello World!0'
[x] Received 'Hello World!1'
[x] Received 'Hello World!2'
[x] Received 'Hello World!3'
[x] Received 'Hello World!4'
[x] Received 'Hello World!5'
[x] Received 'Hello World!6'
[x] Received 'Hello World!7'
[x] Received 'Hello World!8'
[x] Received 'Hello World!9'
此时查看消息队列
$sudo rabbitmqctl list_queues
Listing queues ...
HELLO 0
celeryev.db53a5e0-1e6a-4f06-a9f7-2c104c4612fb 0
...done.
可以看到队列HELLO中的数据被读走了,条数为0。
未完待续
http://www.01happy.com/ubuntu-rabbitmq-and-python-practice/
python 操作消息队列的更多相关文章
- rabbit mq的php使用 php-amqplib操作消息队列
rabbit mq的php使用 php-amqplib操作消息队列 有大神翻译的教程,非常清楚可以参考: https://xiaoxiami.gitbook.io/rabbitmq_into_chin ...
- python之消息队列
引言 你是否遇到过两个(多个)系统间需要通过定时任务来同步某些数据?你是否在为异构系统的不同进程间相互调用.通讯的问题而苦恼.挣扎?如果是,那么恭喜你,消息服务让你可以很轻松地解决这些问题.消息服务擅 ...
- Python RabbitMQ消息队列
python内的队列queue 线程 queue:不同线程交互,不能夸进程 进程 queue:只能用于父进程与子进程,或者同一父进程下的多个子进程,进行交互 注:不同的两个独立进程是不能交互的. ...
- 架构设计之NodeJS操作消息队列RabbitMQ
一. 什么是消息队列? 消息(Message)是指在应用间传送的数据.消息可以非常简单,比如只包含文本字符串,也可以更复杂,可能包含嵌入对象. 消息队列(Message Queue)是一种应用间的通信 ...
- python使用消息队列RabbitMq(入门)
windows平台开发和使用 安装 安装Erlang:https://pan.baidu.com/s/1QcZDaI205uaue7mMWh5cSA 安装RabbitMQ:https://pan.ba ...
- nodejs操作消息队列RabbitMQ
一. 什么是消息队列 消息队列(Message Queue,简称MQ),从字面意思上看,本质是个队列,FIFO先入先出,只不过队列中存放的内容是message而已.其主要用途:不同进程Process/ ...
- Python RabbitMQ 消息队列
RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. MQ全称为Message Queue, 消息队列(MQ)是一种应用程序 ...
- python使用消息队列RabbitMq(进阶)
import pika connection = pika.BlockingConnection(pika.ConnectionParameters( 'localhost')) channel = ...
- php中对共享内存,消息队列的操作
http://www.cnblogs.com/fengwei/archive/2012/09/12/2682646.html php作为脚本程序,通常生命周期都很短,如在web应用中,一次请求就是ph ...
随机推荐
- [知识点]网络流之Dinic算法
// 此博文为迁移而来,写于2015年2月6日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vrg4.html ...
- 2分钟 windows下sublime text 3安装git插件:
12:35 2015/11/182分钟 windows下sublime text 3安装git插件:推荐博客:http://blog.csdn.net/naola2001/article/detail ...
- 10秒钟安装 Vim编辑器,5分钟浏览常用命令 2015.10.25
首先我想说,vim与vi的命令几乎相同,,所以学习编辑命令时很轻松,排除扩展相关,以及自动补全等配置的使用在外30秒钟安装 Vim编辑器,5分钟浏览常用命令环境:虚拟机Ubuntu:安装vim并浏览命 ...
- 【BZOJ】1535: [POI2005]Sza-Template
题意 给一个串\(s(1 \le |s| \le 500000)\),求一个最长的串,使得这个串能覆盖整个串(可以重叠). 分析 首先这个串肯定是前缀也肯定是后缀. 题解 对串kmp后,建立\(fai ...
- This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release.
一,经历 <1> 使用SDWebImage下载 成功图片后,将图片设置给 self.imageView.image,提示如题所示的错误提示. <2>第一反应就是慢慢注释掉代码进 ...
- HDU 2824 简单欧拉函数
1.HDU 2824 The Euler function 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=2824 3.总结:欧拉函数 题意:求(a ...
- 配置1000条ACE的脚本
配置1000条ACE的脚本 测试 python 引言 在路由器或者交换机产品中,手工配置大量shell命令时,难免繁琐且效率低下,鉴于CRT中支持多种脚本语言,因此可通过脚本执行大量重复的shell配 ...
- 关于Autoit上传文件的问题
Autoit上传文件需要安装两个软件: 使用这个做文件上传时:在代码中需要添加的代码如下: Runtime.getRuntime().exec("d:/zhpg.exe"); D盘 ...
- 改进iOS客户端的升级提醒功能
改进iOS客户端的升级提醒功能 功能设计 先申明一下,我是码农,不是一个产品经理,但我觉得现有市面上的很多 App,设计的 "升级提示功能" 都不太友好.在此分享一下我的想法,欢迎 ...
- [LintCode] Happy Number 快乐数
Write an algorithm to determine if a number is happy. A happy number is a number defined by the foll ...