内容简介:本次更新的版本是v0.0.14, 支持通过@rpc装饰器修饰一个类。 下面是一个TCP协议的服务端例子。 创建一个TcpRpcServer对象, 指定服务端监听地址和端口 通过@rpc装饰器注册需要被客户端请求的方法 调用serve()方法,...
本次更新的版本是v0.0.14, 支持通过@rpc装饰器修饰一个类。
下面是一个TCP协议的服务端例子。
- 创建一个TcpRpcServer对象, 指定服务端监听地址和端口
- 通过@rpc装饰器注册需要被客户端请求的方法
- 调用serve()方法,开始处理客户端请求
from agileutil.rpc.server import TcpRpcServer, rpc @rpc class TestService: def hello(self, name): return "Hello, {}!".format(name) def add(self, a, b, c): return a + b + c @rpc def hello(name): return "Hello, {}!".format(name) server = TcpRpcServer('0.0.0.0', 9988) server.serve()
TCP RPC 客户端
- 创建TcpRpcClient对象,指定RPC服务端地址
- 通过call()方法,指定服务端方法名称和参数(注意:如果方法名不存在,或者服务端未调用@rpc装饰器注册,那么call()方法将抛出异常)
- call() 方法的返回值和在本地调用一样,原来是什么返回类型,就还是什么(例如返回字典、列表、对象甚至内置类型,经过序列化后,不会发生改变)
from agileutil.rpc.client import TcpRpcClient cli = TcpRpcClient('127.0.0.1', 9988, timeout = 2) resp = cli.call('TestService.hello', args=('xiaoming',)) print(resp) resp = cli.call('TestService.add', args=(1, 2, 3)) print(resp) resp = cli.call('hello', args=('xiaoming',)) print(resp)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Domain-Driven Design
Eric Evans / Addison-Wesley Professional / 2003-8-30 / USD 74.99
"Eric Evans has written a fantastic book on how you can make the design of your software match your mental model of the problem domain you are addressing. "His book is very compatible with XP. It is n......一起来看看 《Domain-Driven Design》 这本书的介绍吧!