使用zeromq的轻松python rpc
wirepickle的Python项目详细描述
线腌菜
使用pickle通过ZeroMQ轻松调用python远程过程。 允许您在每个传输层上将单个类实例公开为api服务器 ZeroMQ提供的。
快速设置
使用您喜爱的软件包管理器安装Wirepickle:
$ pip install wirepickle
服务器
接下来,在服务器上,用@expose
装饰器注释要公开的方法:
fromwirepickle.serverimportexpose,ServerclassFoo:@expose('bar')defbar(self):print('bar')return0@expose('baz')defbaz(self,arg1,kwarg1='baz'):print(kwarg1)returnself.bar()+arg1
注意,您可以返回任何可以作为返回值进行pickle的内容, 这个stdout将镜像到客户端。
然后,将类的实例传递给Server
构造函数以创建服务器。
要开始侦听,请使用
绑定uri:
if__name__=='__main__':instance=Foo()Server(instance).listen('tcp://*:12345')
客户
只需传递一个要连接的uri并开始使用这些方法。
fromwirepickle.clientimportClientfoo=Client('tcp://127.0.0.1:12345')foo.bar()
如果需要,可以将timeout
kwarg传递给Client
构造函数:
foo=Client('tcp://127.0.0.1:12345',timeout=1_000)# milliseconds