RabbitMQ中的RPC | 阿小信的博客
阿小信大人的头像
Talk is cheap. Show me the code. Linus Torvalds

RabbitMQ中的RPC2014-09-02 06:37

RPC全称Remote Procedure Call。即client发送消息到队列,server从队列取出消息,经过处理后将结果再返回给client。RabbitMQ的Routing、Publish/Subscribe、Worker queue模式都没有返回信息的功能。

rpcclient.py

#!/usr/bin/env python
import pika
import uuid

class FibonacciRpcClient(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):
        if self.corr_id == props.correlation_id:
            self.response = body

    def call(self, n):
        self.response = None
        self.corr_id = str(uuid.uuid4())
        self.channel.basic_publish(exchange='',
                                   routing_key='rpc_queue',
                                   properties=pika.BasicProperties(
                                         reply_to = self.callback_queue,
                                         correlation_id = self.corr_id,
                                         ),
                                   body=str(n))
        while self.response is None:
            self.connection.process_data_events()
        return int(self.response)

fibonacci_rpc = FibonacciRpcClient()

print " [x] Requesting fib(30)"
response = fibonacci_rpc.call(30)
print " [.] Got %r" % (response,)

rpcserver.py

#!/usr/bin/env python
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='localhost'))

channel = connection.channel()

channel.queue_declare(queue='rpc_queue')

def fib(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fib(n-1) + fib(n-2)

def on_request(ch, method, props, body):
    n = int(body)

    print " [.] fib(%s)"  % (n,)
    response = fib(n)

    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(on_request, queue='rpc_queue')

print " [x] Awaiting RPC requests"
channel.start_consuming()
  • client使用reply_to和corralation_id让server处理后知道返回的数据中那个是自己的。
  • prefetch_count参数可以让你开启多个server。
  • basic_consume方法实在收到请求时会自动执行。

如果您觉得从我的分享中得到了帮助,并且希望我的博客持续发展下去,请点击支付宝捐赠,谢谢!

若非特别声明,文章均为阿小信的个人笔记,转载请注明出处。文章如有侵权内容,请联系我,我会及时删除。

#Python#   #queue
分享到:
阅读[1723] 评论[0]

你可能也感兴趣的文章推荐

本文最近访客

网友54.*.*.22[火星]2018-08-21 08:30
网友54.*.*.35[法国]2018-08-21 08:26
网友46.*.*.67[Ashburn]2018-08-21 08:11
网友46.*.*.83[火星]2018-08-21 07:59

发表评论