- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章详解Python操作RabbitMQ服务器消息队列的远程结果返回由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
先说一下笔者这里的测试环境:Ubuntu14.04 + Python 2.7.4 RabbitMQ服务器 。
1
|
sudo
apt-get
install
rabbitmq-server
|
Python使用RabbitMQ需要Pika库 。
1
|
sudo
pip
install
pika
|
远程结果返回 消息发送端发送消息出去后没有结果返回。如果只是单纯发送消息,当然没有问题了,但是在实际中,常常会需要接收端将收到的消息进行处理之后,返回给发送端.
处理方法描述:发送端在发送信息前,产生一个接收消息的临时队列,该队列用来接收返回的结果。其实在这里接收端、发送端的概念已经比较模糊了,因为发送端也同样要接收消息,接收端同样也要发送消息,所以这里笔者使用另外的示例来演示这一过程.
示例内容:假设有一个控制中心和一个计算节点,控制中心会将一个自然数N发送给计算节点,计算节点将N值加1后,返回给控制中心。这里用center.py模拟控制中心,compute.py模拟计算节点.
compute.py代码分析 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
#!/usr/bin/env python
#coding=utf8
import
pika
#连接rabbitmq服务器
connection
=
pika.BlockingConnection(pika.ConnectionParameters(
host
=
'localhost'
))
channel
=
connection.channel()
#定义队列
channel.queue_declare(queue
=
'compute_queue'
)
print
' [*] Waiting for n'
#将n值加1
def
increase(n):
return
n
+
1
#定义接收到消息的处理方法
def
request(ch, method, properties, body):
print
" [.] increase(%s)"
%
(body,)
response
=
increase(
int
(body))
#将计算结果发送回控制中心
ch.basic_publish(exchange
=
'',
routing_key
=
properties.reply_to,
body
=
str
(response))
ch.basic_ack(delivery_tag
=
method.delivery_tag)
channel.basic_qos(prefetch_count
=
1
)
channel.basic_consume(request, queue
=
'compute_queue'
)
channel.start_consuming()
|
计算节点的代码比较简单,值得一提的是,原来的接收方法都是直接将消息打印出来,这边进行了加一的计算,并将结果发送回控制中心.
center.py代码分析 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
#!/usr/bin/env python
#coding=utf8
import
pika
class
Center(
object
):
def
__init__(
self
):
self
.connection
=
pika.BlockingConnection(pika.ConnectionParameters(
host
=
'localhost'
))
self
.channel
=
self
.connection.channel()
#定义接收返回消息的队列
result
=
self
.channel.queue_declare(exclusive
=
True
)
self
.callback_queue
=
result.method.queue
self
.channel.basic_consume(
self
.on_response,
no_ack
=
True
,
queue
=
self
.callback_queue)
#定义接收到返回消息的处理方法
def
on_response(
self
, ch, method, props, body):
self
.response
=
body
def
request(
self
, n):
self
.response
=
None
#发送计算请求,并声明返回队列
self
.channel.basic_publish(exchange
=
'',
routing_key
=
'compute_queue'
,
properties
=
pika.BasicProperties(
reply_to
=
self
.callback_queue,
),
body
=
str
(n))
#接收返回的数据
while
self
.response
is
None
:
self
.connection.process_data_events()
return
int
(
self
.response)
center
=
Center()
print
" [x] Requesting increase(30)"
response
=
center.request(
30
)
print
" [.] Got %r"
%
(response,)
|
上例代码定义了接收返回数据的队列和处理方法,并且在发送请求的时候将该队列赋值给reply_to,在计算节点代码中就是通过这个参数来获取返回队列的.
打开两个终端,一个运行代码python compute.py,另外一个终端运行center.py,如果执行成功,应该就能看到效果了.
笔者在测试的时候,出了些小问题,就是在center.py发送消息时没有指明返回队列,结果compute.py那边在计算完结果要发回数据时报错,提示routing_key不存在,再次运行也报错。用rabbitmqctl list_queues查看队列,发现compute_queue队列有1条数据,每次重新运行compute.py的时候,都会重新处理这条数据。后来使用/etc/init.d/rabbitmq-server restart重新启动下rabbitmq就ok了.
相互关联编号correlation id 上一遍演示了远程结果返回的示例,但是有一个没有提到,就是correlation id,这个是个什么东东呢?
假设有多个计算节点,控制中心开启多个线程,往这些计算节点发送数字,要求计算结果并返回,但是控制中心只开启了一个队列,所有线程都是从这个队列里获取消息,每个线程如何确定收到的消息就是该线程对应的呢?这个就是correlation id的用处了。correlation翻译成中文就是相互关联,也表达了这个意思.
correlation id运行原理:控制中心发送计算请求时设置correlation id,而后计算节点将计算结果,连同接收到的correlation id一起返回,这样控制中心就能通过correlation id来标识请求。其实correlation id也可以理解为请求的唯一标识码.
示例内容:控制中心开启多个线程,每个线程都发起一次计算请求,通过correlation id,每个线程都能准确收到相应的计算结果.
compute.py代码分析 。
和上面一篇相比,只需修改一个地方:将计算结果发送回控制中心时,增加参数correlation_id的设定,该参数的值其实是从控制中心发送过来的,这里只是再次发送回去。代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
#!/usr/bin/env python
#coding=utf8
import
pika
#连接rabbitmq服务器
connection
=
pika.BlockingConnection(pika.ConnectionParameters(
host
=
'localhost'
))
channel
=
connection.channel()
#定义队列
channel.queue_declare(queue
=
'compute_queue'
)
print
' [*] Waiting for n'
#将n值加1
def
increase(n):
return
n
+
1
#定义接收到消息的处理方法
def
request(ch, method, props, body):
print
" [.] increase(%s)"
%
(body,)
response
=
increase(
int
(body))
#将计算结果发送回控制中心,增加correlation_id的设定
ch.basic_publish(exchange
=
'',
routing_key
=
props.reply_to,
properties
=
pika.BasicProperties(correlation_id
=
\
props.correlation_id),
body
=
str
(response))
ch.basic_ack(delivery_tag
=
method.delivery_tag)
channel.basic_qos(prefetch_count
=
1
)
channel.basic_consume(request, queue
=
'compute_queue'
)
channel.start_consuming()
|
center.py代码分析 。
控制中心代码稍微复杂些,其中比较关键的有三个地方:
使用python的uuid来产生唯一的correlation_id。 发送计算请求时,设定参数correlation_id。 定义一个字典来保存返回的数据,并且键值为相应线程产生的correlation_id。 代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
#!/usr/bin/env python
#coding=utf8
import
pika, threading, uuid
#自定义线程类,继承threading.Thread
class
MyThread(threading.Thread):
def
__init__(
self
, func, num):
super
(MyThread,
self
).__init__()
self
.func
=
func
self
.num
=
num
def
run(
self
):
print
" [x] Requesting increase(%d)"
%
self
.num
response
=
self
.func(
self
.num)
print
" [.] increase(%d)=%d"
%
(
self
.num, response)
#控制中心类
class
Center(
object
):
def
__init__(
self
):
self
.connection
=
pika.BlockingConnection(pika.ConnectionParameters(
host
=
'localhost'
))
self
.channel
=
self
.connection.channel()
#定义接收返回消息的队列
result
=
self
.channel.queue_declare(exclusive
=
True
)
self
.callback_queue
=
result.method.queue
self
.channel.basic_consume(
self
.on_response,
no_ack
=
True
,
queue
=
self
.callback_queue)
#返回的结果都会存储在该字典里
self
.response
=
{}
#定义接收到返回消息的处理方法
def
on_response(
self
, ch, method, props, body):
self
.response[props.correlation_id]
=
body
def
request(
self
, n):
corr_id
=
str
(uuid.uuid4())
self
.response[corr_id]
=
None
#发送计算请求,并设定返回队列和correlation_id
self
.channel.basic_publish(exchange
=
'',
routing_key
=
'compute_queue'
,
properties
=
pika.BasicProperties(
reply_to
=
self
.callback_queue,
correlation_id
=
corr_id,
),
body
=
str
(n))
#接收返回的数据
while
self
.response[corr_id]
is
None
:
self
.connection.process_data_events()
return
int
(
self
.response[corr_id])
center
=
Center()
#发起5次计算请求
nums
=
[
10
,
20
,
30
,
40
,
50
]
threads
=
[]
for
num
in
nums:
threads.append(MyThread(center.request, num))
for
thread
in
threads:
thread.start()
for
thread
in
threads:
thread.join()
|
笔者开启了两个终端,来运行compute.py,开启一个终端来运行center.py,最后结果输出截图如下:
可以看到虽然获取的结果不是顺序输出,但是结果和源数据都是对应的.
这边示例的做法就是创建一个队列,使用correlation id来标识每次请求。也有做法可以不使用correlation id,就是每请求一次,就创建一个临时队列,不过这样太消耗性能了,官方也不推荐这么做.
最后此篇关于详解Python操作RabbitMQ服务器消息队列的远程结果返回的文章就讲到这里了,如果你想了解更多关于详解Python操作RabbitMQ服务器消息队列的远程结果返回的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我遇到一种情况,我需要从某个主题读取(正在进行的)消息并将它们放入另一个 Queue 中。我怀疑我是否需要 jms Queue 或者我可以对内存中的 java Queue 感到满意。我将通过同一 jv
队列的定义 队列(Queue):先进先出的线性表 队列是仅在队尾进行插入和队头进行删除操作的线性表 队头(front):线性表的表头端,即可删除端 队尾(rear):线性表的表尾端,即可插入端 由于这
Redis专题-队列 首先,想一想 Redis 适合做消息队列吗? 1、消息队列的消息存取需求是什么?redis中的解决方案是什么? 无非就是下面这几点: 0、数据可以顺序读
0. 学习目标 栈和队列是在程序设计中常见的数据类型,从数据结构的角度来讲,栈和队列也是线性表,是操作受限的线性表,它们的基本操作是线性表操作的子集,但从数据类型的角度来讲,它们与线性表又有着巨大的不
我想在 redis + Flask 和 Python 中实现一个队列。我已经用 RQ 实现了这样的查询,如果你有 Flask 应用程序和任务在同一台服务器上工作,它就可以正常工作。我想知道是否有可能创
我正在使用 Laravel 5.1,我有一个大约需要 2 分钟来处理的任务,这个任务特别是生成报告...... 现在,很明显,我不能让用户在我接受用户输入的同一页面上等待 2 分钟,而是我应该在后台处
我正在使用 Azure 队列,并且有多个不同的进程从队列中读取数据。 我的系统的构建方式假设每条消息只读取一次。 这个Microsoft article声称 Azure 队列具有至少一次传送保证,这可
我正在创建一个Thread::Queue元素数组。 我这样做是这样的: for (my $i=0; $i new; } 但是,当我在每个队列中填充这样的元素时 $queues[$index]->enq
我试图了解如何将我的 Mercurial 补丁推送到远程存储库(例如 bitbucket.org),而不必先应用它们(实际上提交它们)。我的动机是在最终完成之前首先对我的工作进行远程备份,并且能够与其
我的本地计算机上有一个 Mercurial 队列补丁,我需要与同事共享该补丁,但我不想将其提交到上游存储库。有没有一种简单的方法可以打包该补丁并与他分享? 最佳答案 mq 将补丁作为不带扩展名的文
Java 中是否有任何类提供与 Queue 相同的功能,但有返回对象的选项,并且不要删除它,只需将其设置在集合末尾? 最佳答案 Queue不直接提供这样的方法。但是,您可以使用 poll 和 add
我在Windows上使用Tortoise svn客户端,我需要能够一次提交来自不同子文件夹的更改文件-一次提交。像在提交之前将文件添加到队列中之类的?我该怎么做? Windows上是否还有另一个svn
好吧,我正在尝试对我的 DSAQueue 类进行单元测试,它显示我的 isEmpty()、isFull() 和 dequeue() 方法失败。 以下是我的 DSAQueue 代码。我认为我的 Dequ
我想尽量减少对传入请求的数据库查询。它目前需要写入 6 个不同的表。在返回响应之前不需要完成处理。因此,我考虑了 laravel 队列,但我想知道我是否也可以摆脱写入队列/作业表所需的单独查询。我可以
我正在学习队列数据结构。我想用链表创建队列。我想编程输出:10 20程序输出:队列为空-1 队列为空-1 我哪里出错了? 代码如下: class Node { int x; Node next
“当工作人员有空时,他们会根据主题的优先级列表从等待请求池中进行选择。在时间 t 到达的所有请求都可以在时间 t 进行分配。如果两名工作人员同时有空,则安排优先权分配给最近的工作最早安排的人。如果仍然
我正在开发一个巨大的应用程序,它使用一些子菜单、模式窗口、提示等。 现在,我想知道在此类应用程序中处理 Esc 和单击外部事件的正确方法。 $(document).keyup(function(e)
所以 如果我有一个队列 a --> b --> NULL; 当我使用函数时 void duplicate(QueueNodePtr pHead, QueueNodePtr *pTail) 它会给 a
我正在尝试为键盘输入实现 FIFO 队列,但似乎无法让它工作。我可以让键盘输入显示在液晶显示屏上,但这就是我能做的。我认为代码应该读取键盘输入并将其插入队列,然后弹出键盘输入并将值读取到液晶屏幕上。有
我正在学习算法和 DS。如何在 JavaScript 中使用队列? 我知道你可以做这样的事情。 var stack = []; stack.push(2); // stack is now
我是一名优秀的程序员,十分优秀!