# 重构bsonrpc为异步rpc框架 **Repository Path**: xie-chunlong/async-bsonrpc ## Basic Information - **Project Name**: 重构bsonrpc为异步rpc框架 - **Description**: aiobsonrpc, bsonrpc原有方法基础上进行添加修改 - **Primary Language**: Python - **License**: MPL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-02-24 - **Last Updated**: 2023-03-06 ## Categories & Tags **Categories**: Uncategorized **Tags**: Socket ## README # aiobsonrpc - 满足异步的rpc的需求 - 大部分方法的使用与bsonrpc框架相同,只不过需要使用await来异步调用 ## Quickstart #### Example ##### Server ```python import asyncio from bsonrpc import JSONRpc from bsonrpc import rpc_request, request, service_class @service_class class ServerServices(object): @request def echo_times(self, txt, n): return txt * n @rpc_request async def long_process(self, rpc, a, b, c): # print(rpc.client_info) # 2 ways to send notifications: await rpc.invoke_notification('report_progress', 'Stage 1') client = rpc.get_peer_proxy() await client.n.report_progress('Stage 2') # Make a request to client, why not: result = await client.swapper('TestinG') print(result) # -> "GnitseT" return a * b * c async def main(): server = JSONRpc("localhost", 8080, ServerServices()) await server.server_start() await server.close_server() if __name__ == "__main__": loop = asyncio.get_event_loop() loop.run_until_complete(main()) loop.run_forever() ``` ##### Client ```python import asyncio from bsonrpc import BatchBuilder, JSONRpc from bsonrpc import request, notification, service_class @service_class class ClientServices(object): @request def swapper(self, txt): return ''.join(reversed(list(txt))) @notification def report_progress(self, txt): print('Msg from server: ' + txt) async def main(): client = JSONRpc("localhost", 8080, ClientServices()) await client.client_start() rpc = client.get_peer_proxy() bb = BatchBuilder() bb.echo_times('-hello-', 2) bb.echo_times('-world-', 1) batch_result = await client.batch_call(bb) print(batch_result) # -> ['-hello--hello-', '-world-'] print(await rpc.long_process(2, 3, 4)) # -> Msg from server: Stage 1 # -> Msg from server: Stage 2 # -> 24+- await client.close() # Closes the socket 's' also if __name__ == "__main__": asyncio.run(main()) ```