Client端代码:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pika
import uuid
import time class FibonacciRpcClient(object):
def __init__(self):
#生成socket
self.connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
#生成管道
self.channel = self.connection.channel()
#声明一个随机queue,exclusive=True会在此queue的消费者断开后,自动将queue删除
result = self.channel.queue_declare(exclusive=True)
#获取随机queue名
self.callback_queue = result.method.queue
#定义收到消息后的动作
self.channel.basic_consume(self.on_response, #回调函数on_response
no_ack=True,
queue=self.callback_queue) #获取随机queue名 def on_response(self, ch, method, props, body):
if self.corr_id == props.correlation_id: #判断uuid是否是否一致
self.response = body #队列返回 def call(self, n):
self.response = None
self.corr_id = str(uuid.uuid4()) #生成uuid,等会发送给服务端
#发送消息给服务端
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() #非阻塞版的start_consuming(),如果收到消息就执行on_response回调函数
print("no msg....")
time.sleep(0.5) #这里可以执行其他命令
return int(self.response) #返回结果 #生成实例
fibonacci_rpc = FibonacciRpcClient() print("[x] Requesting fib(30)") #调用call函数
response = fibonacci_rpc.call(30) print("[x] got %r " % response)

server端代码:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pika
import time #生成socket
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
#生成管道
channel = connection.channel()
#声明一个queue防止启动报错
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') channel.start_consuming()

Python通过RabbitMQ实现RPC的更多相关文章

  1. Python操作RabbitMQ

    RabbitMQ介绍 RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现的产品,RabbitMQ是一个消息代理,从“生产者”接收消息并传递消 ...

  2. 用 Python、 RabbitMQ 和 Nameko 实现微服务

    用 Python. RabbitMQ 和 Nameko 实现微服务 原创 07-17 17:57 首页 Linux中国 "微服务是一股新浪潮" - 现如今,将项目拆分成多个独立的. ...

  3. 十一天 python操作rabbitmq、redis

    1.启动rabbimq.mysql 在""运行""里输入services.msc,找到rabbimq.mysql启动即可 2.启动redis 管理员进入cmd, ...

  4. Python之RabbitMQ的使用

    今天总结一下Python关于Rabbitmq的使用 RabbitMQ官网说明,其实也是一种队列,那和前面说的线程queue和进程queue有什么区别呢? 线程queue只能在同一个进程下进行数据交互 ...

  5. python操作rabbitmq、redis

    1.启动rabbimq.mysql 在“”运行“”里输入services.msc,找到rabbimq.mysql启动即可 2.启动redis 管理员进入cmd,进入redis所在目录,执行redis- ...

  6. RabbitMQ 实现RPC

    实现RPC 首先要弄明白,RPC是个什么东西. (RPC) Remote Procedure Call Protocol 远程过程调用协议 在一个大型的公司,系统由大大小小的服务构成,不同的团队维护不 ...

  7. python之RabbitMQ

    一.安装RabbitMQ 1. 安装erlang 1 2 3 4 tar xf otp_src_18.3.tar.gz cd otp_src_18.3 ./configure --prefix=/ma ...

  8. Python之路【第九篇】:Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy

    Python之路[第九篇]:Python操作 RabbitMQ.Redis.Memcache.SQLAlchemy   Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用 ...

  9. python - 操作RabbitMQ

    python - 操作RabbitMQ     介绍 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议.MQ全称为Mess ...

随机推荐

  1. java——super关键字、final关键字、throws关键字、访问控制

    super关键字: 当父类被重写之后,子类对象无法访问父类被重写的方法,super就是为了解决这个问题: 1.使用super关键字访问父类的成员变量和成员方法: super.成员变量 super.成员 ...

  2. leetcoe--47. Permutations II

    1.问题描述 Given a collection of numbers that might contain duplicates, return all possible unique permu ...

  3. CentOS Linux解决Device eth0 does not seem to be present【转】

    在VMware里克隆出来的CentOS Linux,ifconfig...没有看到eth0,然后重启网卡又报下面错误. 故障现象: service network restartShutting do ...

  4. CQRS之旅——旅程1(我们的领域:Contoso会议管理系统)

    旅程1:我们的领域:Contoso会议管理系统 起点:我们从哪里来,我们带来了什么,谁将与我们同行?" 只要前进,我愿意去任何地方." --大卫•利文斯通 本章介绍了一个虚构的公司 ...

  5. NLog学习笔记一

    一.NLog是什么? NLog是一个基于.NET的免费的开源的日志记录类库.(官网:http://nlog-project.org/) NLog特点如下: 配置简单方便.可以将配置信息写的应用程序的配 ...

  6. 《C#高效编程》读书笔记09-避免在API中使用转换操作符

    转换操作符为类之间引入了一种"可替换性"(substitutability)."可替换性"表示一个类的实例可以替换为另一个类的实例. public class ...

  7. 前端-页面性能调试:Hiper

    前端-页面性能调试:Hiper   我们写单页面应用,想看页面修改后性能变更其实挺繁琐的.有时想知道是「正优化」还是「负优化」只能靠手动刷新查看network.而Hiper很好解决了这一痛点(其实Hi ...

  8. 判断dataset表中是否存在 某列

    DataSet ds ; ds.Tables[0].Columns.Contains("a") 同样适用于 datarow dr ; dr.Table.Columns.Contai ...

  9. 从零开始的全栈工程师——js篇2.2

    条件语句 补充: var a=“hello world” a这个变量是字符串了 对于里面每一个字母来说 他是字节 里面有11个字节 字节总数用length表示 如下: 根据上面的内容咱们又发现了一个运 ...

  10. sass相关随笔

    安装 下载ruby并且安装 点击这里 打开命令行输入 gem install sass 我使用的是sublime text3 还需要下载三个插件 sass -- 可以帮助你语法高亮 sass buil ...