Package: lacia
LaciaA modernJson-Rpc/Bson-Rpcimplementation, compatible withJson-Rpc 2.0andJson-Rpc X, supports multiple network protocols and backend frameworks and supports bidirectional calls..人间总有一两风，填我十万八千梦安装pipinstalllaciapdmaddlacia特性跨语言Python3JavaScriptGoC/C++JavaRust多种网络协议支持HTTPWebSocket兼容 Json-Rpc 2.0 规范兼容 Json-Rpc X 规范支持完备的链式调用自动 Json-Rpc 规范转换双向流式传输支持 BSONIDE 支持 (#1)分布式Server使用入门Server 端importasynciofromlaciaimportJsonRpc,AioServerexpose={'add':lambdaa,b:a+b,'sub':lambdaa,b:a-b,'value':'jsonrpc Server 0.0.1'}loop=asyncio.new_event_loop()rpc=JsonRpc('/test',namespace=expose,loop=loop)rpc.run_server(AioServer())Client 端importasynciofromlaciaimportJsonRpc,AioClient,loggerloop=asyncio.new_event_loop()rpc=JsonRpc('/test',loop=loop)asyncdefmain():awaitrpc.run_client(AioClient())res1=awaitrpc.valueres2=awaitrpc.add(4,4)logger.info(res1)logger.info(res2)loop.run_until_complete(main())loop.run_forever()链式调用Server 端importasynciofromlaciaimportJsonRpc,AioServerclassnumber:def__init__(self)->None:self.value=0defadd(self,i:int):self.value+=ireturnselfdefsub(self,i:int):self.value-=ireturnselfexpose={'number':number}loop=asyncio.new_event_loop()rpc=JsonRpc('/test',namespace=expose,loop=loop)rpc.run_server(AioServer())Client 端importasynciofromlaciaimportJsonRpc,AioClient,loggerloop=asyncio.new_event_loop()rpc=JsonRpc('/test',loop=loop)asyncdefmain():awaitrpc.run_client(AioClient())res3=awaitrpc.number().add(10).sub(10).valuelogger.info(res3)loop.run_until_complete(main())loop.run_forever()反向调用Server 端importasynciofromlaciaimportJsonRpc,AioServerexpose={'value':'jsonrpc Server 0.0.1'}loop=asyncio.new_event_loop()rpc=JsonRpc('/test',namespace=expose,loop=loop)asyncdefrepeat():res=awaitrpc.valuereturnresrpc.add_namespace('repeat',repeat)rpc.run_server(AioServer())Client 端importasynciofromlaciaimportJsonRpc,AioClient,loggerexpose={'value':'jsonrpc Client 0.0.1'}loop=asyncio.new_event_loop()rpc=JsonRpc('/test',namespace=expose,loop=loop)asyncdefmain():awaitrpc.run_client(AioClient())res4=awaitrpc.repeat()logger.info(res4)loop.run_until_complete(main())loop.run_forever()流式调用Server 端importasynciofromlaciaimportJsonRpc,AioServerasyncdefasync_generator(i=10):foriinrange(i):awaitasyncio.sleep(0.5)yieldiexpose={'async_generator':async_generator}loop=asyncio.new_event_loop()rpc=JsonRpc('/test',namespace=expose,loop=loop)rpc.run_server(AioServer())Client 端importasynciofromlaciaimportJsonRpc,AioClient,loggerloop=asyncio.new_event_loop()rpc=JsonRpc('/test',loop=loop)asyncdefmain():awaitrpc.run_client(AioClient())asyncforiinrpc.async_generator(5):# type: ignorelogger.info(i)loop.run_until_complete(main())loop.run_forever()IDE 支持(实验性)Server 端importasynciofromlaciaimportJsonRpc,AioServerasyncdefasync_generator(i:int=10):foriinrange(i):awaitasyncio.sleep(0.5)yieldiexpose={'async_generator':async_generator}loop=asyncio.new_event_loop()rpc=JsonRpc('/test',namespace=expose,loop=loop)rpc.rpc.generate_pyi('CustomSchema')# Generate Pyi file in ./laciaschema/CustomSchema.pyirpc.run_server(AioServer())Client 端importasynciofromlaciaimportJsonRpc,AioClient,loggerfromlaciaschema.CustomSchemaimportCustomSchemaloop=asyncio.new_event_loop()irpc=JsonRpc('/test',loop=loop)rpc=irpc.with_schema(CustomSchema)# Use CustomSchema as schema andasyncdefmain():awaitirpc.run_client(AioClient())asyncforiinrpc.async_generator(5):# type: ignorelogger.info(i)loop.run_until_complete(main())loop.run_forever()IDE 现只支持函数和基础类型, Class 正在重构中