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 ...
随机推荐
- eclipse 高亮配对的括号
在编辑代码框右键->preference,直接就可以看到Matching brackets highlights
- 测试经验总结(“二次开发”)
参考原文地址:http://www.51testing.com/html/86/n-3601786.html 不知不觉已经工作五年时间了,从茁壮.康拓普.利鑫云端到现在的惠卡世纪,经历过机顶盒软件(嵌 ...
- Codeforces Beta Round #4 (Div. 2 Only)
A题,水题,准1Y,第一次CE了..CF里,CE没有罚时.. B题,直接模拟.. #include <cstdio> #include <string> #include &l ...
- C#_生成HTML
#region 生成静态页 /// <summary> /// 生成静态页 /// </summary> /// <param name="URL"& ...
- 基于 Node.js 平台,快速、开放、极简的 web 开发框架。
资料地址:http://www.expressjs.com.cn/ Express 基于 Node.js 平台,快速.开放.极简的 web 开发框架. $ npm install express -- ...
- iosiPhone屏幕尺寸、分辨率及适配
iosiPhone屏幕尺寸.分辨率及适配 1.iPhone尺寸规格 设备 iPhone 宽 Width 高 Height 对角线 Diagonal 逻辑分辨率(point) Scale Fac ...
- find命令详解
find命令详解 来源: ChinaUnix博客 日期: 2008.07.25 16:04 (共有条评论) 我要评论 [url=http://www.sudu.cn/web/host.php] ...
- 20145337实验四Android开发基础
20145337实验四Android开发基础 实验内容 基于Android Studio开发简单的Android应用并部署测试; 了解Android组件.布局管理器的使用: 掌握Android中事件处 ...
- Python开发问题和解决方案汇集
1.Sublime Text中用Tab批量替换空格Whitespace缩进:Ctrl+A全选代码,Ctrl+Shift+P打开下拉框,输入indent,找到Convert indentation to ...
- IOS网络第二天 - 01-基本的HTTP请求
***************** #import "HMViewController.h" #import "MBProgressHUD+MJ.h" @int ...