rpchandler 和 rpcproxy 的基本思路是很比较简单的。 如果一个客户端想要调用一个远程函数,比如 foo(1, 2, z=3) ,代理类创建一个包含了函数名和参数的元组 (‘foo', (1, 2), {‘z': 3}) 。 这个元组被pickle序列化后通过网络连接发生出去。 这一步在 rpcproxy 的 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 rpcproxy:
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 = rpcproxy(c)
res = proxy.add(2, 3)
print(res)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!