RabbitMQ中的许多客户端和许多服务器

时间:2014-09-26 17:08:00

标签: python rabbitmq rpc

我看了this你可以解释几件事吗? 我已经在终端的不同标签(3个标签页)中运行了 rpc_server.py

该教程中的

rpc_server.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()

很好,我需要发送 send.py 3个请求:

#!/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,)

fibonacci_rpc1 = FibonacciRpcClient()

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

fibonacci_rpc2 = FibonacciRpcClient()

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

是否意味着脚本将等待第一个请求的响应,然后发送第二个请求,再次等待响应,然后发送第三个请求?

我想在一瞬间做3个请求,不等待响应然后发送新请求。这该怎么做?

我如何更改send.py或使用其他技巧?我必须使用线程或多处理吗? RabbitMQ是否支持此功能?

谢谢!

1 个答案:

答案 0 :(得分:0)

如果您希望一次发送所有三个请求,则需要使用线程。一个简单的解决方案看起来像这样:

import threading
from time import sleep

def make_rpc_call(value):
    fibonacci_rpc = FibonacciRpcClient()
    print " [x] Requesting fib({0})".format(value)
    response = fibonacci_rpc.call(value)
    print " [.] Got %r" % (response,)

for index in xrange(5):
    thread_ = threading.Thread(target=make_rpc_call, args=(30, ))
    thread_.start()
    sleep(0.1)

请记住,Pika不是线程安全的,因此您需要为每个线程创建一个连接。作为替代方案,您可以查看我的Psk的Flask示例here。修改它很容易,允许您异步执行多个请求。

def rpc_call():
    # Fire of all three requests.
    corr_id1 = rpc_client.send_request('1')
    corr_id2 = rpc_client.send_request('2')
    corr_id3 = rpc_client.send_request('3')

    # Wait for the response on all three requests.
    while not rpc_client.queue[corr_id1] \
            or not rpc_client.queue[corr_id2] \
            or not rpc_client.queue[corr_id3]:
        sleep(0.1)

    # Print the result of all three requests.
    print rpc_client.queue[corr_id1]
    print rpc_client.queue[corr_id2]
    print rpc_client.queue[corr_id3]

if __name__ == '__main__':
    rpc_client = RpcClient('rpc_queue')
    sleep(1)
    print rpc_call()