# -*- coding:utf-8 -*- import pika,time credentials = pika.PlainCredentials('guest', 'guest') connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.199.11',5672,'/',credentials)) channel = connection.channel() channel.queue_declare(queue='rpc_queue') def func(n): name = "results" return name def on_request(ch, method,props,body): n = body #(n为生产者传过来的message) print(" [消费者-服务端执行命令] fib(%s)" % n) response = func(n) #收到信息,执行什么 ch.basic_publish(exchange='', routing_key=props.reply_to, #客户端随机生成的Q properties=pika.BasicProperties(correlation_id= props.correlation_id), body=response) ch.basic_ack(delivery_tag=method.delivery_tag) channel.basic_qos(prefetch_count=1) # 设定当队列中有1条消息的时候,不再接受新消息 channel.basic_consume(queue='rpc_queue', on_message_callback=on_request ) print(" [消费者--服务端] 等待接受指令") channel.start_consuming()