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 ...
随机推荐
- C#--使用存储过程
过程: 1.连接数据库 2.设置执行类型为存储过程,区别于SQL语句 3.得到执行结果,返回值.输出参数等 //连接数据库 string connstr = "Data Source=ZGC ...
- Kafka——分布式消息系统
Kafka——分布式消息系统 架构 Apache Kafka是2010年12月份开源的项目,采用scala语言编写,使用了多种效率优化机制,整体架构比较新颖(push/pull),更适合异构集群. 设 ...
- Python之路【第八篇】python实现线程池
线程池概念 什么是线程池?诸如web服务器.数据库服务器.文件服务器和邮件服务器等许多服务器应用都面向处理来自某些远程来源的大量短小的任务.构建服务器应用程序的一个过于简单的模型是:每当一个请求到达就 ...
- gradle providedCompile 与compile区别
Gradle compile: 如果你的jar包/依赖代码 在编译的时候需要依赖,在运行的时候也需要,那么就用compile例如 : compile 'org.springframework.boot ...
- java获取指定路径下的指定文件/java.io.File.listFiles(FilenameFilter filter)
java.io.File.listFiles(FilenameFilter filter) 返回抽象路径名数组,表示在目录中此抽象路径名表示,满足指定过滤器的文件和目录. 声明 以下是java.io. ...
- mysql存储过程之异常处理篇
mysql存储过程也提供了对异常处理的功能:通过定义HANDLER来完成异常声明的实现 语法如下: DECLARE handler_type HANDLER FOR condition_value[, ...
- linux的bash 终端操作快捷键
bash中的快捷键操作: 分为两大类, 一是移动, 另一个是删除 一. 移动 1. 按字符移动: ctrl+f, ctrl+b 3. 按行移动: ctrl+a, ctrl+e 二. 删除: 1. 按字 ...
- 安装windows后重新修复grub2的引导
前段时间,我的用的双系统windows8.1 + fedora 21 workstation,使用grub2引导fedora和windows8.1的启动.由于一些原因,导致我的windows8.1无法 ...
- LINUX系统知识(转)
原文链接:http://blog.chinaunix.net/uid-725717-id-2060377.html 在Linux上配置好svnserve,通过eclipse访问,实现版本控制.但是开启 ...
- spring 缓存(spring自带Cache)(入门)
spring的缓存机制,是方法纬度的缓存机制, 这就意味着我们并不用关注 底层是否使用了数据库以及通过什么方式访问的数据库: 因此,此缓存方法既适用于dao层,也适用于service层. spring ...