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 ...
随机推荐
- HTML5 本地存储 localStorage、sessionStorage 的遍历、存储大小限制处理
HTML5 的本地存储 API 中的 localStorage 与 sessionStorage 在使用方法上是相同的,区别在于 sessionStorage 在关闭页面后即被清空,而 localSt ...
- Leetcode Add Binary
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- 原生js动画效果(源码解析)
在做页面中,多数情况下都会遇到页面上做动画效果,大部分都是用jquery来实现动画,今天正好看到一篇原生js实现动画效果的代码,特分享在此. 原文地址:http://www.it165.net/pro ...
- python 之redis
redis是一个key-value存储系统,与memcached类似,它支持存储到value类型相对更多,包括string(字符串),list(列表),set(集合),zset(sorted set ...
- [LintCode] Segment Tree Build 建立线段树
The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...
- 1021上课演练----SQL注入与避免(银行系统)
package com.bank; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.Dat ...
- Qweb Pdf 中添加 图片
具体方法如下: <img t-if="company.logo" t-att-src="'data:image/png;base64,%s' % company.l ...
- JCreator的配置
1.在Configure(配置)菜单上选择Options(选项),将弹出对话框. 2.在Option对话框中选择左侧JDK Profile,选择右侧JDK version 1.x.x.. ,点击Edi ...
- BootStrap 模态框基本用法
<!DOCTYPE html> <html> <head> <title>Bootstrap 实例 - 模态框(Modal)插件方法</title ...
- super()和this()的区别
1)调用super()必须写在子类构造方法的第一行,否则编译不通过.每个子类构造方法的第一条语句,都是隐含地调用super(),如果父类没有这种形式的构造函数,那么在编译的时候就会报错. 2)supe ...