RPC:Remote Producre Call
远程 过程 调用
str(uuid.uuid4()) 唯一标志符
队列谁先声明谁先启动
命令+& 异步 fg 查看正在执行的任务
fg+序列号 查看对应的任务 [ 序列号 ]
import pika import uuid class FibonacciRpcClient(object): def __init__(self): credentials = pika.PlainCredentials('alex', 'alex123') self.connection = pika.BlockingConnection(pika.ConnectionParameters( '192.168.14.52', credentials=credentials)) channel = self.connection.channel() 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): """"callback方法""" 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)) count = 0 while self.response is None: self.connection.process_data_events() #检查队列里有没有新消息,但不会阻塞 count +=1 print("check...",count) return int(self.response) fibonacci_rpc = FibonacciRpcClient() print(" [x] Requesting fib(30)") response = fibonacci_rpc.call(5) print(" [.] Got %r" % response)
import pika import time credentials = pika.PlainCredentials('alex', 'alex123') connection = pika.BlockingConnection(pika.ConnectionParameters( '192.168.14.52',credentials=credentials)) 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()