Python遠(yuǎn)程方法調(diào)用實(shí)現(xiàn)過程解析
RPCHandler 和 RPCProxy 的基本思路是很比較簡(jiǎn)單的。 如果一個(gè)客戶端想要調(diào)用一個(gè)遠(yuǎn)程函數(shù),比如 foo(1, 2, z=3) ,代理類創(chuàng)建一個(gè)包含了函數(shù)名和參數(shù)的元組 (‘foo', (1, 2), {‘z': 3}) 。 這個(gè)元組被pickle序列化后通過網(wǎng)絡(luò)連接發(fā)生出去。 這一步在 RPCProxy 的 getattr() 方法返回的 do_rpc() 閉包中完成。
服務(wù)器接收后通過pickle反序列化消息,查找函數(shù)名看看是否已經(jīng)注冊(cè)過,然后執(zhí)行相應(yīng)的函數(shù)。 執(zhí)行結(jié)果(或異常)被pickle序列化后返回發(fā)送給客戶端。實(shí)例需要依賴 multiprocessing 進(jìn)行通信。 不過,這種方式可以適用于其他任何消息系統(tǒng)。例如,如果你想在ZeroMQ之上實(shí)習(xí)RPC, 僅僅只需要將連接對(duì)象換成合適的ZeroMQ的socket對(duì)象即可。
先實(shí)現(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')
再實(shí)現(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)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Windows下Pycharm遠(yuǎn)程連接虛擬機(jī)中Centos下的Python環(huán)境(圖文教程詳解)
- mac 上配置Pycharm連接遠(yuǎn)程服務(wù)器并實(shí)現(xiàn)使用遠(yuǎn)程服務(wù)器Python解釋器的方法
- 使用 Python ssh 遠(yuǎn)程登陸服務(wù)器的最佳方案
- 使用Python實(shí)現(xiàn)Wake On Lan遠(yuǎn)程開機(jī)功能
- Pycharm使用遠(yuǎn)程linux服務(wù)器conda/python環(huán)境在本地運(yùn)行的方法(圖解))
- Python 中的 import 機(jī)制之實(shí)現(xiàn)遠(yuǎn)程導(dǎo)入模塊
- python 采用paramiko 遠(yuǎn)程執(zhí)行命令及報(bào)錯(cuò)解決
相關(guān)文章
Python實(shí)現(xiàn)朗讀在線音頻和本地音頻
在日常的Python軟件開發(fā)中,我們經(jīng)常會(huì)遇到一個(gè)非常重要的功能需求——讓程序能夠讀取并顯示文本內(nèi)容,下面我們就來學(xué)習(xí)一下Python實(shí)現(xiàn)朗讀音頻的具體操作吧2024-03-03
Python優(yōu)化技巧之利用ctypes提高執(zhí)行速度
ctypes是Python的一個(gè)外部庫,提供和C語言兼容的數(shù)據(jù)類型,可以很方便地調(diào)用C DLL中的函數(shù)。今天我們就來詳細(xì)探討下ctypes庫的使用技巧2016-09-09
python numpy.power()數(shù)組元素求n次方案例
這篇文章主要介紹了python numpy.power()數(shù)組元素求n次方案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03
使用python os模塊復(fù)制文件到指定文件夾的方法
今天小編就為大家分享一篇使用python os模塊復(fù)制文件到指定文件夾的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08
使用PyTorch實(shí)現(xiàn)手寫數(shù)字識(shí)別功能
在人工智能的世界里,計(jì)算機(jī)視覺是最具魅力的領(lǐng)域之一,通過PyTorch這一強(qiáng)大的深度學(xué)習(xí)框架,我們將在經(jīng)典的MNIST數(shù)據(jù)集上,見證一個(gè)神經(jīng)網(wǎng)絡(luò)從零開始學(xué)會(huì)識(shí)別數(shù)字的全過程,本文給大家介紹了如何使用PyTorch實(shí)現(xiàn)手寫數(shù)字識(shí)別,需要的朋友可以參考下2025-03-03

