# RPC-DEMO **Repository Path**: yunl2435/RPC-DEMO ## Basic Information - **Project Name**: RPC-DEMO - **Description**: 一个用python编写的rpc实现的demo - **Primary Language**: Python - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 4 - **Forks**: 1 - **Created**: 2020-07-19 - **Last Updated**: 2023-12-04 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # RPC-DEMO #### 描述 这是一个基于`python`编写的rpc调用的实现demo,支持返回值和异步回调。 #### 使用方法 只需要将类名作为参数传输服务端和客户端接口,并绑定相同的ip和端口,即可远程调用。 1. 编写自定义的类 ```python class RPCTest(object): def __init__(self, value='RPCTest'): self._value = value def say_hello(self): print 'say_hello called' return 'hello world' def my_add(self, val): print 'my_add called' return self._value + ' ' + val ``` 2. 在一个控制台中启动服务端 ```python from rpc.Server import Server from test.netTest import RPCTest s = Server(RPCTest, 'TestNetwork') s.setup(ip='localhost', port=3200) s.run() ``` 3. 在另一个控制台中启动服务端即可进行调用 ```python from rpc.Client import Client from test.netTest import RPCTest def callback_test(): print 'callback called' c = Client(RPCTest) c.connect(ip='localhost', port=3200) print c.say_hello(callback=callback_test) for i in range(10): print c.my_add('ha' * i) c.close() ``` 4. 输出 * 服务端输出 ```bash (, ('127.0.0.1', 6831)) say_hello called my_add called my_add called my_add called my_add called my_add called my_add called my_add called my_add called my_add called my_add called ``` * 客户端输出 ```bash callback called hello world TestNetwork TestNetwork ha TestNetwork haha TestNetwork hahaha TestNetwork hahahaha TestNetwork hahahahaha TestNetwork hahahahahaha TestNetwork hahahahahahaha TestNetwork hahahahahahahaha TestNetwork hahahahahahahahaha ```