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端代码分析(原创)的更多相关文章

  1. 推荐一款最强Python自动化神器!不用写一行代码!

    搞过自动化测试的小伙伴,相信都知道,在Web自动化测试中,有一款自动化测试神器工具: selenium.结合标准的WebDriver API来编写Python自动化脚本,可以实现解放双手,让脚本代替人 ...

  2. 推荐一款最强Python自动化神器!再也不用写代码了!

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理 搞过自动化测试的小伙伴,相信都知道,在Web自动化测试中,有一款自动化测试神器工具: seleniu ...

  3. Appium+python自动化(三十)- 实现代码与数据分离 - 数据配置-yaml(超详解)

    简介 本篇文章主要介绍了python中yaml配置文件模块的使用让其完成数据和代码的分离,宏哥觉得挺不错的,于是就义无反顾地分享给大家,也给大家做个参考.一起跟随宏哥过来看看吧. 思考问题 前面我们配 ...

  4. tls 双向认证 client端代码例子

    example: python import httplib import json import ssl import urllib2 import requests CA_FILE = " ...

  5. Appium+python自动化(三十二)- 代码写死一时爽,框架重构火葬场 - PageObject+unittest(超详解)

    简介 江湖有言:”代码写死一时爽,框架重构火葬场“,更有人戏言:”代码动态一时爽,一直动态一直爽

  6. swoole 异步非堵塞 server/端 client/端 代码,已经测试完毕。贴代码

    服务器环境  centos7.0  swoole4.3 php7.2 pcre4.8  nginx1.8   php-fpm server.php <?php class Server { pr ...

  7. [深度学习]Python/Theano实现逻辑回归网络的代码分析

    2014-07-21 10:28:34 首先PO上主要Python代码(2.7), 这个代码在Deep Learning上可以找到. # allocate symbolic variables for ...

  8. 有没有一个工具可以帮助查找python的bug和进行静态的代码分析?

    答:PyChecker是一个python代码的静态分析工具,它可以帮助查找python代码的bug, 会对代码的复杂度和格式提出警告 Pylint是另外一个工具可以进行codingstandard检查

  9. Python实例---抽屉热搜榜前端代码分析

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

随机推荐

  1. Winsock 入门 Echo 示例

    #include <stdio.h> #include <winsock2.h> #pragma comment(lib, "ws2_32") /* Win ...

  2. php json_decode

    php代码 <?php $data='[{"Name":"a1","Number":"123","Con ...

  3. Java序列化的几种方式以及序列化的作用

    Java序列化的几种方式以及序列化的作用 本文着重讲解一下Java序列化的相关内容. 如果对Java序列化感兴趣的同学可以研究一下. 一.Java序列化的作用    有的时候我们想要把一个Java对象 ...

  4. MVC缓存OutPutCache学习笔记 (一) 参数配置

    OutPutCache 参数详解 Duration : 缓存时间,以秒为单位,这个除非你的Location=None,可以不添加此属性,其余时候都是必须的. Location : 缓存放置的位置; 该 ...

  5. Sphinx 之 Coreseek、Sphinx-for-chinaese、Sphinx+Scws 评测

    Sphinx是一个基于SQL的全文检索引擎:普遍使用于很多网站:但由于中英文的差异,其本身,对中文的支持并不好.主要体现在对一段话断词:英文只需按照空格对其分词即可:但对于博大精深的中文来说,却是件困 ...

  6. java 使用BeanUtils.copyProperties(Object source,Object target) 复制字段值

    BeanUtils.copyProperties(person, wsPerson);把person的字段值,复制给wsPerson // 只复制两个实体中,字段名称一样的 很有用的一个功能...

  7. Linux命令之dos2unix

    Linux命令之dos2unix (2011-09-22 11:24:06) 转载▼ 标签: 杂谈   Linux命令之dos2unix - 将DOS格式文本文件转换成UNIX格式 用途说明 dos2 ...

  8. the usage of linux command "expect"

    #! /usr/bin/expect -f# this script is used to practise the command "expect" #when "li ...

  9. NUGet的诞生与使用

    本文引用地址:http://msdn.microsoft.com/zh-cn/magazine/hh547106.aspx NuGet 使用 NuGet 管理项目库 Phil Haack 无论多么努力 ...

  10. spring 缓存(spring自带Cache)(入门)源码解读

    spring自带的缓存类有两个基础类:Cache(org.springframework.cache.Cache)类,CacheManager(org.springframework.cache.Ca ...