Python自动化之rabbitmq rpc client端代码分析(原创)
RPC调用client端解析
import pika
import uuid
# 建立连接
class FibonacciRpcClient(object):
def __init__(self):
# 建立建立连接和通道
self.connection = pika.BlockingConnection(pika.ConnectionParameters(host="localhost"))
self.channel = self.connection.channel()
# exclusive(专有的): Only allow access by the current connection
result = self.channel.queue_declare(exclusive=True)
# 获取队列名称
self.callback_queue = result.method.queue
# 接收服务端的回应
# param on_response:The function for dispatching messages to user, having the signature:
"""
Start a queue consumer.
This method asks the server to start a "consumer",
which is a transient request for messages from a specific queue.
Consumers last as long as the channel they were declared on, or until the client cancels them.
"""
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())
"""This method publishes a message to a specific exchange.
The message will be routed to queues as defined by the exchange configuration
and distributed to any active consumers when the transaction, if any, is committed."""
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)
)
# 确认是否有收到消息,没有的话阻塞在这里
# Will make sure that data events are processed. Dispatches timer and
# channel callbacks if not called from the scope of BlockingConnection or
# BlockingChannel callback. Your app can block on this method.
# while self.response is None: # 跟start_consuming相似
# 是一个等待消息的阻塞过程,连接的任何消息都可以使它脱离阻塞状态(有点像Ajax的事件等待机制)
while self.response is None:
self.connection.process_data_events()
return int(self.response)
ssh_rpc = FibonacciRpcClient()
response = ssh_rpc.call(30)
# Processes(处理) I/O events and dispatches timers and `basic_consume`
# callbacks until all consumers are cancelled."""
# 循环接收我们的消息,接收之后并执行我们的callback函数
channel.start_consuming()
Python自动化之rabbitmq rpc client端代码分析(原创)的更多相关文章
- 推荐一款最强Python自动化神器!不用写一行代码!
搞过自动化测试的小伙伴,相信都知道,在Web自动化测试中,有一款自动化测试神器工具: selenium.结合标准的WebDriver API来编写Python自动化脚本,可以实现解放双手,让脚本代替人 ...
- 推荐一款最强Python自动化神器!再也不用写代码了!
本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理 搞过自动化测试的小伙伴,相信都知道,在Web自动化测试中,有一款自动化测试神器工具: seleniu ...
- Appium+python自动化(三十)- 实现代码与数据分离 - 数据配置-yaml(超详解)
简介 本篇文章主要介绍了python中yaml配置文件模块的使用让其完成数据和代码的分离,宏哥觉得挺不错的,于是就义无反顾地分享给大家,也给大家做个参考.一起跟随宏哥过来看看吧. 思考问题 前面我们配 ...
- tls 双向认证 client端代码例子
example: python import httplib import json import ssl import urllib2 import requests CA_FILE = " ...
- Appium+python自动化(三十二)- 代码写死一时爽,框架重构火葬场 - PageObject+unittest(超详解)
简介 江湖有言:”代码写死一时爽,框架重构火葬场“,更有人戏言:”代码动态一时爽,一直动态一直爽
- swoole 异步非堵塞 server/端 client/端 代码,已经测试完毕。贴代码
服务器环境 centos7.0 swoole4.3 php7.2 pcre4.8 nginx1.8 php-fpm server.php <?php class Server { pr ...
- [深度学习]Python/Theano实现逻辑回归网络的代码分析
2014-07-21 10:28:34 首先PO上主要Python代码(2.7), 这个代码在Deep Learning上可以找到. # allocate symbolic variables for ...
- 有没有一个工具可以帮助查找python的bug和进行静态的代码分析?
答:PyChecker是一个python代码的静态分析工具,它可以帮助查找python代码的bug, 会对代码的复杂度和格式提出警告 Pylint是另外一个工具可以进行codingstandard检查
- Python实例---抽屉热搜榜前端代码分析
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
随机推荐
- js 限制input输入字节长度
function WidthCheck(str, maxLen){ var w = 0; var tempCount = 0; //length 获取字数数,不区分汉子和英文 for (var i=0 ...
- 20145212 实验四《Andoid开发基础》
20145212 实验四<Andoid开发基础> 实验内容 安装Android Studio 运行安卓AVD模拟器 使用Android运行出模拟手机并显示自己的学号 实验过程 一.安装An ...
- HTML学习笔记——frameset和marquee
1>frameset 效果如百Google度 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...
- 给IOS系统的微信页面赋Title
给页面赋一个title是最平常不过的事情了,不过在IOS下动态给页面赋title可不是平常的事情. 看代码: function setIOStitle(title) { $body = $('body ...
- css 父层 透明 子层不透明Alpha
html代码 <div class="user2-register-bg"> <div class="user2-register-con"& ...
- php的if语句单行与多行
//正确: //错误写法 $b = //if前面不能带 等号
- xhprof安装使用
安装: 到pecl官网下载xhprof的最新版:http://pecl.php.net/package/xhprof wget http://pecl.php.net/get/xhprof-0.9.4 ...
- imageserver
https://bitbucket.org/tamtam-nl/tamtam-nuget-imageserver/overview https://www.nuget.org/packages/Tam ...
- MVC5的AuthorizeAttribute详解
现今大多数的网站尤其是购物网站都要求你登录后才能继续操作,当你匿名的将商品放入购物车后,不可能匿名购买这时可以转到登录界面让用户进行登录验证. 适用系统自带的过滤器 MVC5只要将属性[Authori ...
- 使用微信JS-SDK调用微信浏览器的接口
先附上微信公众平台的相关链接: 微信公众平台:https://mp.weixin.qq.com/ 微信公众平台开发文档:https://mp.weixin.qq.com/wiki 微信公众平台JS-S ...