Python远程方法调用实现过程解析
作者:Mars.wang 时间:2022-12-24 01:26:45
RPCHandler 和 R * roxy 的基本思路是很比较简单的。 如果一个客户端想要调用一个远程函数,比如 foo(1, 2, z=3) ,代理类创建一个包含了函数名和参数的元组 (‘foo', (1, 2), {‘z': 3}) 。 这个元组被pickle序列化后通过网络连接发生出去。 这一步在 R * roxy 的 getattr() 方法返回的 do_rpc() 闭包中完成。
服务器接收后通过pickle反序列化消息,查找函数名看看是否已经注册过,然后执行相应的函数。 执行结果(或异常)被pickle序列化后返回发送给客户端。实例需要依赖 multiprocessing 进行通信。 不过,这种方式可以适用于其他任何消息系统。例如,如果你想在ZeroMQ之上实习RPC, 仅仅只需要将连接对象换成合适的ZeroMQ的socket对象即可。
先实现server端
import json
from multiprocessing.connection import Listener
from threading import Thread
class RPCHandler:
def __init__(self):
self._functions = {}
def register_function(self, func):
self._functions[func.__name__] = func
def handle_connection(self, connection):
try:
while True:
func_name, args, kwargs = json.loads(connection.recv())
# Run the RPC and send a response
try:
r = self._functions[func_name](*args, **kwargs)
connection.send(json.dumps(r))
except Exception as e:
connection.send(json.dumps(e))
except EOFError:
pass
def rpc_server(handler, address, authkey):
sock = Listener(address, authkey=authkey)
while True:
client = sock.accept()
t = Thread(target=handler.handle_connection, args=(client,))
t.daemon = True
t.start()
# Some remote functions
def add(x,y):
return x+y
if __name__ == '__main__':
handler = RPCHandler()
handler.register_function(add)
# Run the server
rpc_server(handler, ('127.0.0.1', 17000), authkey=b'peekaboo')
再实现client端
import json
from multiprocessing.connection import Client
class R * roxy:
def __init__(self, connection):
self._connection = connection
def __getattr__(self, name):
def do_rpc(*args, **kwargs):
self._connection.send(json.dumps((name, args, kwargs)))
result = json.loads(self._connection.recv())
if isinstance(result, Exception):
raise result
return result
return do_rpc
if __name__ == '__main__':
c = Client(('127.0.0.1', 17000), authkey=b'peekaboo')
proxy = R * roxy(c)
res = proxy.add(2, 3)
print(res)
来源:https://www.cnblogs.com/wangbin2188/p/13177065.html
标签:Python,远程,方法,调用
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
你喜欢篮球吗?Python实现篮球游戏
2023-10-26 00:28:36
![](https://img.aspxhome.com/file/2023/6/68456_0s.png)
Python使用中文正则表达式匹配指定中文字符串的方法示例
2021-11-28 22:17:00
![](https://img.aspxhome.com/file/2023/0/86430_0s.png)
使用线框图来简化你的产品设计流程
2011-06-10 13:10:00
Python 统计列表中重复元素的个数并返回其索引值的实现方法
2023-07-15 12:31:24
![](https://img.aspxhome.com/file/2023/9/77679_0s.png)
Python常见数据类型转换操作示例
2022-11-20 19:13:05
在TensorFlow中实现矩阵维度扩展
2021-09-21 17:50:15
pytorch实现focal loss的两种方式小结
2023-07-02 14:43:22
Python标准库os.path包、glob包使用实例
2021-09-04 13:49:03
python中xlwt模块的具体用法
2023-11-08 07:10:15
![](https://img.aspxhome.com/file/2023/4/92324_0s.png)
python管理包路径之pycharm自动解决包路径注册
2023-07-14 14:25:38
![](https://img.aspxhome.com/file/2023/4/100294_0s.png)
Python脚本实现网卡流量监控
2022-11-27 11:36:51
openai createChatCompletion函数使用实例
2023-06-14 16:45:38
Perl中的正则表达式介绍
2023-08-11 21:14:25
Python实现简单的"导弹" 自动追踪原理解析
2021-04-15 05:48:27
![](https://img.aspxhome.com/file/2023/9/110549_0s.png)
python PyTorch预训练示例
2022-07-06 18:24:02
Django 通过JS实现ajax过程详解
2023-08-17 08:00:46
Django中login_required装饰器的深入介绍
2023-08-22 09:18:28
确定能够释放空间的SQL Server数据库文件的脚本
2010-07-31 12:36:00
![](https://img.aspxhome.com/file/UploadPic/20107/31/2010731132357283s.jpg)
Pickle模块中的dump()和load()方法简介
2023-03-21 04:18:06
![](https://img.aspxhome.com/file/2023/3/98883_0s.png)
上传组件 ASPUpload 使用说明
2008-09-11 14:38:00