【脚本语言系列】关于Python远程调用,你需要知道的事
来源:互联网 发布:青岛中山路美食 知乎 编辑:程序博客网 时间:2024/06/06 03:42
如何使用远程过程调用
- 服务器端
- 提供函数
- 绑定地址和端口
- 注册函数
- 启动服务器并等待
——- - 获取被调用函数名
- 调用函数并返回结果
- 客户端
- 连接服务器
- 调用函数
——- - 接收结果
xmlrpc
# -*- coding:utf-8 -*-import xmlrpclibfrom SimpleXMLRPCServer import SimpleXMLRPCServer# serverdef double(num): return num * 2server = SimpleXMLRPCServer(("localhost",6789))server.register_function(double, "double")server.serve_forever()
# -*- coding:utf-8 -*-import xmlrpclib# clientproxy = xmlrpclib.ServerProxy("http://localhost:6789/")num = 7result = proxy.double(num)print("Double %s is %s" %(num, result))
msgpackrpc
# -*- coding:utf-8 -*-from msgpackrpc import Server, Address# serverclass Services(): def double(num): return num * 2server = Server(Services()) server.listen(Address("localhost",6789))server.start()
# -*- coding:utf-8 -*-from msgpackrpc import Client, Address# clientclient = Client(Address("http://localhost",6789))num = 7result = client.call("double",num)print("Double %s is %s" %(num, result))
fabric
# -*- coding:utf-8 -*-from fabric.api import rundef iso(): run('date -u')# fab -f temp_server.py -H localhost iso
salt(基于ZeroMQ,fabric)
puppet
chef
ansible(fabric)
什么是远程过程调用
远程过程调用(RPC)类似在联网的远程机器上,运行函数。
RPC由RPC客户端和远程机器组成;
RPC客户端的工作原理
1. 编码/编组/序列化,即把函数参数转化为比特
2. 数据发送,即把比特发送给远程机器
- 数据接收,即接收转码后的比特
- 反序列化,即把比特转化为函数返回值
远程机器的工作原理 - 数据接收,即接收转码后的比特
- 反序列化,即把比特转化为函数参数
- 函数调用,即查找本地目标函数并调用,之后输出函数返回值
- 编码/编组/序列化,即把函数返回值转化为比特
5 数据发送,即把比特发送给RPC客户端
为何使用远程过程调用
通过联网的机器,可以操作更多的进程和线程。
阅读全文
0 0
- 【脚本语言系列】关于Python远程调用,你需要知道的事
- 【脚本语言系列】关于Python系统管理,你需要知道的事
- 【脚本语言系列】关于Python容器,你需要知道的事
- 【脚本语言系列】关于Python调试报错,你需要知道的事
- 【脚本语言系列】关于Python开发环境搭建,你需要知道的事
- 【脚本语言系列】关于Python数值计算NumPy, 你需要知道的事
- 【脚本语言系列】关于Python测试工具lettuce, 你需要知道的事
- 【脚本语言系列】关于Python测试工具mock, 你需要知道的事
- 【脚本语言系列】关于Python测试工具unittest, 你需要知道的事
- 【脚本语言系列】关于Python测试工具docstrings, 你需要知道的事
- 【脚本语言系列】关于Python测试工具Pyflakes, 你需要知道的事
- 【脚本语言系列】关于Python测试工具Pylint, 你需要知道的事
- 【脚本语言系列】关于Python性能分析工具timeit, 你需要知道的事
- 【脚本语言系列】关于Python代码分析工具line_profiler,你需要知道的事
- 【脚本语言系列】关于Python游戏工具Pygame,你需要知道的事
- 【脚本语言系列】关于Python机器学习scikits-learn,你需要知道的事
- 【脚本语言系列】关于Python数值计算Pandas,你需要知道的事
- 【脚本语言系列】关于Python统计分析statsmodel,你需要知道的事
- 算法题 100:快速售票系统的座位登记
- POJ 1004 Financial Management
- head fiest 设计模式第一章
- VueJs—常用操作手册
- 动态规划——不同的路径
- 【脚本语言系列】关于Python远程调用,你需要知道的事
- 当你在浏览器中输入Google.com并且按下回车之后发生了什么?
- 替换空格
- 哈夫曼树 (c语言)数据结构
- Valgrind内存泄漏检查及定位利器
- Eclipse搭建HTML5开发环境
- 和数能表示1~23的5个正整数
- TensorFlow入门程序-线性回归
- Unity游戏UI框架(七):消息传递中心