python---RabbitMQ(5)消息RPC(远程过程调用)

服务器端:

import pika

#创建socket
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()

客户端:

import pika
import uuid


class FibonacciRpcClient(object):
    def __init__(self):
        #生成socket连接
        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)  
    
#注意,在这里不使用
start_consuming去获取数据,因为这样会堵塞再这里,我们使用了另一种方法self.connection.process_data_events()

def on_response(self, ch, method, props, body): print(
"on_response") 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: print("process_data_events start") self.connection.process_data_events() print("process_data_events end") return int(self.response) fibonacci_rpc = FibonacciRpcClient() print(" [x] Requesting fib(30)") response = fibonacci_rpc.call(30) print(" [.] Got %r" % response)

注意:

self.connection.process_data_events()会去队列中获取处理数据事件,当数据来临的时候,会直接去调用回调函数去处理数据

process_data_events start
process_data_events end
process_data_events start
process_data_events end
process_data_events start
process_data_events end
process_data_events start
process_data_events end
process_data_events start
process_data_events end
process_data_events start
process_data_events end
process_data_events start
process_data_events end
process_data_events start
process_data_events end
process_data_events start
process_data_events end
process_data_events start
process_data_events end
process_data_events start
process_data_events end
process_data_events start  #事件到来
on_response  #调用回调函数去处理数据
process_data_events end  #事件结束
 [.] Got 832040

猜你喜欢

转载自www.cnblogs.com/ssyfj/p/9198020.html