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 ...
随机推荐
- 使用 Git 和 Visual Studio Online 进行版本控制
参考资料: 在开发计算机上设置 Git(配置.创建.克隆.添加) 关于 Git 和 Visual Studio Online 是什么请自行百度 转载请注明来源: http://www.cnblogs. ...
- ACM: Gym 100935B Weird Cryptography - 简单的字符串处理
Weird Cryptography Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- 【BZOJ1968】【AHoi2005】COMMON约数研究
Description Input 只有一行一个整数 N(0 < N < 1000000). Output 只有一行输出,为整数M,即f(1)到f(N)的累加和. Sample Input ...
- Android --日期控件使用并显示选择的日期
1. main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: ...
- Flex Flash Player回声消除的最佳方法
Adobe Flash Player 已经成为音频和视频播放的非常流行的工具.实际上,目前大多数因特网视频均使用 Flash Player观看. Flash Player 通过将许多技术进行组合可以提 ...
- 28个MongoDB 的问题
MongoDB是目前最好的面向文档的免费开源NoSQL数据库.如果你正准备参加MongoDB NoSQL数据库的技术面试,你最好看看下面的MongoDB NoSQL面试问答.这些MongoDB NoS ...
- 附件上传 使用javascript
<html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat=&quo ...
- zabbix3.2.0beta2 监控模版
Zabbix监控中用到了一系列模版,nginx后端检测状态 微信告警等一系列常规的服务应用监控 memcached监控模版,可以自己重新定义memcached的端口 http://files.cnbl ...
- Windows Phone 8 MVVM
Why? 1 reason MVVM works really well with XAML based applications is because of the powerful XAML bi ...
- Bin Packing
Bin Packing 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/F 题目: A set of ...