Python通过RabbitMQ实现RPC
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的更多相关文章
- Python操作RabbitMQ
RabbitMQ介绍 RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现的产品,RabbitMQ是一个消息代理,从“生产者”接收消息并传递消 ...
- 用 Python、 RabbitMQ 和 Nameko 实现微服务
用 Python. RabbitMQ 和 Nameko 实现微服务 原创 07-17 17:57 首页 Linux中国 "微服务是一股新浪潮" - 现如今,将项目拆分成多个独立的. ...
- 十一天 python操作rabbitmq、redis
1.启动rabbimq.mysql 在""运行""里输入services.msc,找到rabbimq.mysql启动即可 2.启动redis 管理员进入cmd, ...
- Python之RabbitMQ的使用
今天总结一下Python关于Rabbitmq的使用 RabbitMQ官网说明,其实也是一种队列,那和前面说的线程queue和进程queue有什么区别呢? 线程queue只能在同一个进程下进行数据交互 ...
- python操作rabbitmq、redis
1.启动rabbimq.mysql 在“”运行“”里输入services.msc,找到rabbimq.mysql启动即可 2.启动redis 管理员进入cmd,进入redis所在目录,执行redis- ...
- RabbitMQ 实现RPC
实现RPC 首先要弄明白,RPC是个什么东西. (RPC) Remote Procedure Call Protocol 远程过程调用协议 在一个大型的公司,系统由大大小小的服务构成,不同的团队维护不 ...
- 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 ...
- Python之路【第九篇】:Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy
Python之路[第九篇]:Python操作 RabbitMQ.Redis.Memcache.SQLAlchemy Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用 ...
- python - 操作RabbitMQ
python - 操作RabbitMQ 介绍 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议.MQ全称为Mess ...
随机推荐
- Jmeter4.0----HTTP Cookie管理器_抓取cookie中的参数(13)
1.说明 请求结束后,要通过登录用户的JSESSIONID判断用户是否登录成功 2.步骤 第一步:添加 HTTP Cookie管理器 录制前,创建”线程组”,线程组=>配置元件=>HTTP ...
- View转换为Bitmap及getDrawingCache
View组件显示的内容可以通过cache机制保存为bitmap, 使用到的api有 void setDrawingCacheEnabled(boolean flag), Bitmap get ...
- java实现的各种hash加密
public class jiami { public static void main(String args[]){ String signString="Thank you!" ...
- UnityError AnimationEvent 'NewEvent' has no receiver! Are you missing a component?
- python操作json文件
import json class OperationJson(object): def __init__(self,file_name=None): if file_name: self.file_ ...
- Murano package
Murano have 2 package types: HOT package with Heat template inside and MuranoPL package with MuranoP ...
- java多线程基础(二)--java多线程的基本使用
java多线程的基本使用 在java中使用多线程,是通过继承Thread这个类或者实现Runnable这个接口或者实现Callable接口来完成多线程的. 下面是很简单的例子代码: package c ...
- PHP 中获取文件名及路径
1. basename("/mnt/img/image01.jpg")函数:得到文件名;输出结果为:image01.jpg. 使用 basename($uriString) 我们可 ...
- Unity C# 运用 GetSaveFileName() 导出Excel文件
本文原创,转载请注明出处:http://www.cnblogs.com/AdvancePikachu/p/6944870.html 唉哟,这次厉害咯,网上搜罗了好久,终于被我找到汉化的保存对话框了,根 ...
- UICollectionView笔记1
WWDC 2012 Session笔记——205 Introducing Collection Views 这是博主的WWDC2012笔记系列中的一篇,完整的笔记列表可以参看这里.如果您是首次来到本站 ...