Python如何實現(xiàn)遠(yuǎn)程方法調(diào)用
問題
你想在一個消息傳輸層如 sockets 、multiprocessing connections 或 ZeroMQ 的基礎(chǔ)之上實現(xiàn)一個簡單的遠(yuǎn)程過程調(diào)用(RPC)。
解決方案
將函數(shù)請求、參數(shù)和返回值使用pickle編碼后,在不同的解釋器直接傳送pickle字節(jié)字符串,可以很容易的實現(xiàn)RPC。 下面是一個簡單的PRC處理器,可以被整合到一個服務(wù)器中去:
# rpcserver.pyimport pickleclass RPCHandler: def __init__(self): self._functions = { } def register_function(self, func): self._functions[func.__name__] = func def handle_connection(self, connection): try: while True:# Receive a messagefunc_name, args, kwargs = pickle.loads(connection.recv())# Run the RPC and send a responsetry: r = self._functions[func_name](*args,**kwargs) connection.send(pickle.dumps(r))except Exception as e: connection.send(pickle.dumps(e)) except EOFError: pass
要使用這個處理器,你需要將它加入到一個消息服務(wù)器中。你有很多種選擇, 但是使用 multiprocessing 庫是最簡單的。下面是一個RPC服務(wù)器例子:
from multiprocessing.connection import Listenerfrom threading import Threaddef 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 functionsdef add(x, y): return x + ydef sub(x, y): return x - y# Register with a handlerhandler = RPCHandler()handler.register_function(add)handler.register_function(sub)# Run the serverrpc_server(handler, (’localhost’, 17000), authkey=b’peekaboo’)
為了從一個遠(yuǎn)程客戶端訪問服務(wù)器,你需要創(chuàng)建一個對應(yīng)的用來傳送請求的RPC代理類。例如
import pickleclass RPCProxy: def __init__(self, connection): self._connection = connection def __getattr__(self, name): def do_rpc(*args, **kwargs): self._connection.send(pickle.dumps((name, args, kwargs))) result = pickle.loads(self._connection.recv()) if isinstance(result, Exception):raise result return result return do_rpc
要使用這個代理類,你需要將其包裝到一個服務(wù)器的連接上面,例如:
>>> from multiprocessing.connection import Client>>> c = Client((’localhost’, 17000), authkey=b’peekaboo’)>>> proxy = RPCProxy(c)>>> proxy.add(2, 3)5>>> proxy.sub(2, 3)-1>>> proxy.sub([1, 2], 4)Traceback (most recent call last): File '<stdin>', line 1, in <module> File 'rpcserver.py', line 37, in do_rpc raise resultTypeError: unsupported operand type(s) for -: ’list’ and ’int’>>>
要注意的是很多消息層(比如 multiprocessing )已經(jīng)使用pickle序列化了數(shù)據(jù)。 如果是這樣的話,對 pickle.dumps() 和 pickle.loads() 的調(diào)用要去掉。
討論
RPCHandler 和 RPCProxy 的基本思路是很比較簡單的。 如果一個客戶端想要調(diào)用一個遠(yuǎn)程函數(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 進(jìn)行通信。 不過,這種方式可以適用于其他任何消息系統(tǒng)。例如,如果你想在ZeroMQ之上實習(xí)RPC, 僅僅只需要將連接對象換成合適的ZeroMQ的socket對象即可。
由于底層需要依賴pickle,那么安全問題就需要考慮了 (因為一個聰明的黑客可以創(chuàng)建特定的消息,能夠讓任意函數(shù)通過pickle反序列化后被執(zhí)行)。 因此你永遠(yuǎn)不要允許來自不信任或未認(rèn)證的客戶端的RPC。特別是你絕對不要允許來自Internet的任意機(jī)器的訪問, 這種只能在內(nèi)部被使用,位于防火墻后面并且不要對外暴露。
作為pickle的替代,你也許可以考慮使用JSON、XML或一些其他的編碼格式來序列化消息。 例如,本機(jī)實例可以很容易的改寫成JSON編碼方案。還需要將 pickle.loads() 和 pickle.dumps() 替換成 json.loads() 和 json.dumps() 即可:
# jsonrpcserver.pyimport jsonclass RPCHandler: def __init__(self): self._functions = { } def register_function(self, func): self._functions[func.__name__] = func def handle_connection(self, connection): try: while True:# Receive a messagefunc_name, args, kwargs = json.loads(connection.recv())# Run the RPC and send a responsetry: r = self._functions[func_name](*args,**kwargs) connection.send(json.dumps(r))except Exception as e: connection.send(json.dumps(str(e))) except EOFError: pass# jsonrpcclient.pyimport jsonclass 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()) return result return do_rpc
實現(xiàn)RPC的一個比較復(fù)雜的問題是如何去處理異常。至少,當(dāng)方法產(chǎn)生異常時服務(wù)器不應(yīng)該奔潰。 因此,返回給客戶端的異常所代表的含義就要好好設(shè)計了。 如果你使用pickle,異常對象實例在客戶端能被反序列化并拋出。如果你使用其他的協(xié)議,那得想想另外的方法了。 不過至少,你應(yīng)該在響應(yīng)中返回異常字符串。我們在JSON的例子中就是使用的這種方式。
以上就是Python如何實現(xiàn)遠(yuǎn)程方法調(diào)用的詳細(xì)內(nèi)容,更多關(guān)于Python遠(yuǎn)程方法調(diào)用的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 以PHP代碼為實例詳解RabbitMQ消息隊列中間件的6種模式2. Python如何進(jìn)行時間處理3. python web框架的總結(jié)4. 詳解Python模塊化編程與裝飾器5. Python通過format函數(shù)格式化顯示值6. html小技巧之td,div標(biāo)簽里內(nèi)容不換行7. python裝飾器三種裝飾模式的簡單分析8. Python 如何將integer轉(zhuǎn)化為羅馬數(shù)(3999以內(nèi))9. Python實現(xiàn)迪杰斯特拉算法過程解析10. python使用ctypes庫調(diào)用DLL動態(tài)鏈接庫
