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 ...
随机推荐
- Android学习笔记——MixLayout
该工程的功能是实现LinearLayout+TableLayout 以下代码是MainActivity.java中的代码 package com.example.mixlayout; import a ...
- ecshop 后台-》广告
1.后台广告宽度限制不能超过1024,高度大于1,admin/ad_position.php 第236行 || $ad_width < ) { make_json_error($_LANG['w ...
- Yii2-admin RBAC权限管理的实现
原文地址:http://www.open-open.com/lib/view/open1434638805348.html http://wlzyan.blog.163.com/blog/stat ...
- margin小结
一. margin百分比 1. 普通元素的百分比margin都是相对于容器的宽度计算 2. 绝对定位元素的百分比margin是相对于第一个定位祖先元素(relative/absolute/fixed) ...
- Task异常处理
http://www.cnblogs.com/xray2005/archive/2011/08/24/2151459.html
- 在ASP.MVC中使用Ajax
Asp.net MVC 抛弃了Asp.net WebForm那种高度封装的控件,让我们跟底层的HTML有了更多的亲近.可以更自由.更灵活的去控制HTML的结构.样式和行为.Asp.net MVC可以更 ...
- JQuery Easy Ui dataGrid 数据表格
数据表格 - DataGrid 英文文档:http://www.jeasyui.com/documentation/index.php# 继承$.fn.panel.defaults,使用$.fn.da ...
- scp命令的用法详解
这篇文章主要是参考了http://blog.csdn.net/jiangkai_nju/article/details/7338177这个博客,要看详细的内容可以参考这个博客进行学习研究,但是我觉得在 ...
- JavaScript实现联想校招员工信息展示
原文摘自我的前端博客,欢迎大家来访问 http://www.hacke2.cn 起因 今天和豪哥聊天,才知道他是我老乡,而且特别近..真的感觉他是我的贵人,这是他从 联想校招扣出来的,我们就用Java ...
- ASO优化总结(基于网络分享的知识总结归纳)
如何优化应用标题? 注意关键字的长度,尽量保证每一个关键字小于10个字符.保持快速更新,因为每次更新,你都将有机会删除表现不佳的关键字以 及增添新的关键字.在ASO中使用关键字的正确做法 标题,并非越 ...