python第十一天学习记录
来源:互联网 发布:男女合租 知乎 编辑:程序博客网 时间:2024/04/30 19:25
主要学习了rabbitmq和redis的使用
其中,使用rabbitmq实现rpc通信:
服务端的实现:
import pikaimport timeconnection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.207.136'))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 pikaimport uuidclass FibonacciRpcClient(object): def __init__(self): self.connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.207.136')) 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)
0 0
- python第十一天学习记录
- python学习---第十一天
- 学习python的第十一天
- Python快速学习第十一天--Python多线程
- python第十天学习记录
- python学习第十三天记录
- python第十五天学习记录
- python第十七天学习记录
- python 第一天学习记录
- Python学习记录--第一天的练习
- 程序学习第十一天
- Java学习第十一天
- .net学习第十一天
- JAVA学习第十一天
- rhce学习第十一天
- 第十一天学习笔记
- Java学习第十一天
- Java学习第十一天
- js字符串截取函数slice()、substring()、substr()
- 单点登录
- 层叠选择框的实现(实现方法参照本博客的下拉列表)
- 带条件的Insert语句
- Xcode7 如何使用代码对齐插件XAlign
- python第十一天学习记录
- 如何把工程部署在Tomcat上
- 如何在vmware中安装mac OS系统
- xUtils3的简单介绍
- kali 2016.2 安装vm-tools
- 前后端分离的探索(二)
- XAlign:用于代码对齐的Xcode插件
- 第八周—OJ分数序列求和
- screenX与clientX区别