异步msgpack rpc
aio-msgpack-rpc的Python项目详细描述
aio msgpack rpc
纯异步实现MsgPack RPC。
使用asyncio
标准库提供的transports, protocols和streams。
只是一个简单的rpc层实现。
安装
pip install aio-msgpack-rpc
示例
服务器
importasyncioimportaio_msgpack_rpc# handlers can be defined on a class# they can either be async or plain functionsclassMyServicer:asyncdefsum(self,x,y):print(f"sum: {x}, {y}")returnx+ydefnotification(self,msg):print(f"notification: {msg}")asyncdefmain():try:server=awaitasyncio.start_server(aio_msgpack_rpc.Server(MyServicer()),host="localhost",port=18002)whileTrue:awaitasyncio.sleep(0.1)finally:server.close()try:asyncio.get_event_loop().run_until_complete(main())exceptKeyboardInterrupt:pass
客户
importasyncioimportaio_msgpack_rpcasyncdefmain():client=aio_msgpack_rpc.Client(*awaitasyncio.open_connection("localhost",18002))# blocking rpc callsresult=awaitclient.call("sum",1,2)assertresult==3# one way notificationsclient.notify("notification","hello")asyncio.get_event_loop().run_until_complete(main())
基准
与我的开发机器上的官方实现相对应的一些基本性能基准。
package | call (QPS) | notify (QPS) |
---|---|---|
msgpack-rpc-python | 5414 | 11746 |
aio-msgpack-rpc | 5781 | 86957 |