Python遠程方法調(diào)用實現(xiàn)過程解析
RPCHandler 和 RPCProxy 的基本思路是很比較簡單的。 如果一個客戶端想要調(diào)用一個遠程函數(shù),比如 foo(1, 2, z=3) ,代理類創(chuàng)建一個包含了函數(shù)名和參數(shù)的元組 (‘foo', (1, 2), {‘z': 3}) 。 這個元組被pickle序列化后通過網(wǎng)絡(luò)連接發(fā)生出去。 這一步在 RPCProxy 的 getattr() 方法返回的 do_rpc() 閉包中完成。
服務(wù)器接收后通過pickle反序列化消息,查找函數(shù)名看看是否已經(jīng)注冊過,然后執(zhí)行相應(yīng)的函數(shù)。 執(zhí)行結(jié)果(或異常)被pickle序列化后返回發(fā)送給客戶端。實例需要依賴 multiprocessing 進行通信。 不過,這種方式可以適用于其他任何消息系統(tǒng)。例如,如果你想在ZeroMQ之上實習RPC, 僅僅只需要將連接對象換成合適的ZeroMQ的socket對象即可。
先實現(xiàn)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')
再實現(xiàn)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)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
- Windows下Pycharm遠程連接虛擬機中Centos下的Python環(huán)境(圖文教程詳解)
- mac 上配置Pycharm連接遠程服務(wù)器并實現(xiàn)使用遠程服務(wù)器Python解釋器的方法
- 使用 Python ssh 遠程登陸服務(wù)器的最佳方案
- 使用Python實現(xiàn)Wake On Lan遠程開機功能
- Pycharm使用遠程linux服務(wù)器conda/python環(huán)境在本地運行的方法(圖解))
- Python 中的 import 機制之實現(xiàn)遠程導(dǎo)入模塊
- python 采用paramiko 遠程執(zhí)行命令及報錯解決
相關(guān)文章
使用Anaconda創(chuàng)建Python指定版本的虛擬環(huán)境的教程詳解
由于工作的需要和學(xué)習的需要,需要創(chuàng)建不同Python版本的虛擬環(huán)境,所以這篇文章主要為大家詳細介紹了如何使用Anaconda創(chuàng)建Python指定版本的虛擬環(huán)境,需要的可以參考下2024-03-03Python 讀取串口數(shù)據(jù),動態(tài)繪圖的示例
今天小編就為大家分享一篇Python 讀取串口數(shù)據(jù),動態(tài)繪圖的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07淺談配置OpenCV3 + Python3的簡易方法(macOS)
下面小編就為大家分享一篇淺談配置OpenCV3 + Python3的簡易方法(macOS),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04Python使用pandas導(dǎo)入xlsx格式的excel文件內(nèi)容操作代碼
這篇文章主要介紹了Python使用pandas導(dǎo)入xlsx格式的excel文件內(nèi)容,基本導(dǎo)入是在Python中使用pandas導(dǎo)入.xlsx文件的方法是read_excel(),本文結(jié)合示例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-12-12Python 實現(xiàn)一個簡單的web服務(wù)器
這篇文章主要介紹了Python 實現(xiàn)一個簡單的web服務(wù)器的方法,幫助大家更好的理解和學(xué)習python,感興趣的朋友可以了解下2021-01-01