使用RabbitMQ接收消息然后处理它然后发回结果

时间:2016-06-27 15:49:54

标签: python rabbitmq

我想从脚本发送消息(直接)而不是处理它,然后发回结果。 所以它就像是双重发布 - 订阅。

我有两个脚本:

  • Processer
  • 客户端

客户端直接向Processser发送消息(简单字符串),然后由Processer脚本计算字符串中的字符并将结果发送回客户端。

这就是我尝试的方式:

Processer等待消息,计算某些内容,然后回复原始发件人。

#Processer.py:
import pika
import sys

#Sends back the score
#addr: Connection address
#exchName: Exchange name (where to send)
#rKey: Name of the queue for direct messages
#score: The detected score
def SendActualScore(addr, exchName, rKey, score):
    #Send the image thru the created channel with the given routing key (queue name)
    channel.basic_publish(exchange=exchName, routing_key=rKey, body=score)
    print "(*) Sent: " + score

#When we receive something this is called
def CallbackImg(ch, method, properties, body):
    print "(*) Received: " + str(body)
    score = str(len(body))
    #Send back the score
    SendActualScore('localhost', 'valami', rKey, score)


#Subscribe connection
#Receive messages thru this
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
#RECEIVE MESSAGES - Subscribe
channel.exchange_declare(exchange='valami', type='direct')
#Define a queue, where we don't need the name
#After we disconnected delete the queue (exclusive flag)
result = channel.queue_declare(exclusive=True)
#We need the name of our temporary queue
queue_name = result.method.queue

rKeys = sys.argv[1:]
for rKey in rKeys:
    channel.queue_bind(exchange='valami', queue=queue_name, routing_key = rKey)

channel.basic_consume(CallbackImg, queue=queue_name, no_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()

客户端只是发送消息而不是等待答案。

#Client.py:
import pika
import sys

connAddr = 'localhost'

#Establish connection
connection = pika.BlockingConnection(pika.ConnectionParameters(connAddr))
channel = connection.channel()

#Define an exchange channel, we don't need a queue
channel.exchange_declare(exchange='valami', type='direct')

#Send the image thru the created channel
channel.basic_publish(exchange='valami', routing_key='msg', body='Message in the body')

print "[*] Sent"

def Callback(ch, method, properties, body):
    print "(*) Received: " + str(body)

result = channel.queue_declare(exclusive=True)
#We need the name of our temporary queue
queue_name = result.method.queue

channel.queue_bind(exchange='valami', queue=queue_name)

channel.basic_consume(Callback, queue=queue_name, no_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()

可能有多个客户端,我不知道如何直接将消息发回给他们。

1 个答案:

答案 0 :(得分:1)

你有没有检查过RabbitMQ中的RPC和python和pika的教程? http://www.rabbitmq.com/tutorials/tutorial-six-python.html

您需要在客户端中执行的操作的要点可以在RPC教程中找到,但需要进行一些修改。

在您的客户端中,您需要创建一个独占队列 - 就像在服务器中一样。

当您从客户端发送消息时,您需要将reply_to设置为客户端专用队列的名称

来自教程:

channel.basic_publish(exchange='',
                      routing_key='rpc_queue',
                      properties=pika.BasicProperties(
                            reply_to = callback_queue,
                            ),
                      body=request)

在服务器上,当您收到消息时,需要从消息中读取reply_to标头,然后basic_publish对该队列的回复。

而不是考虑"客户"和"服务器",根据"消息生成器"来构建它可能会有所帮助。和"消息使用者"。

在您的方案中,您需要将两个流程同时作为发布者和使用者。 "客户"将发布原始邮件并使用响应。 "服务器"将使用原始邮件并发布回复。

您的代码中唯一真正的区别是在原始邮件上使用reply_to标头。这是您应该将响应发布到的队列的名称。

希望有所帮助!

P.S。我在我的RabbitMQ Patterns电子书中涵盖了这个核心大纲 - RPC和请求/回复就像你需要的那样。本书讲的是原理和模式,而不是特定的编程语言(尽管我主要编写node.js而且并不熟悉python)。