项目作者: wo1fsea

项目描述 :
a Python RPC framework using redis
高级语言: Python
项目地址: git://github.com/wo1fsea/PyEasyRPC.git
创建时间: 2019-07-29T11:47:59Z
项目社区:https://github.com/wo1fsea/PyEasyRPC

开源协议:MIT License

下载


PyEasyRPC

PyPI Status
Build Status
codecov

PyEaseRPC is a Python RPC framework easy to use, which using Redis as backend.

Example

Server

  1. # example_server.py
  2. from pyeasyrpc.rpc import remote_method
  3. from pyeasyrpc.rpc import RPCService
  4. class ServiceInstance(RPCService):
  5. @remote_method
  6. def add(self, a, b):
  7. return a + b
  8. @remote_method
  9. def sub(self, a, b):
  10. return a - b
  11. @remote_method
  12. def make_dict(self, **kwargs):
  13. return dict(kwargs)
  14. @remote_method
  15. def make_list(self, *args):
  16. return list(args)
  17. def main():
  18. instance0 = ServiceInstance(process_request_in_thread=True)
  19. instance0.start_background_running()
  20. input("press any key to stop")
  21. instance0.stop_background_running()
  22. if __name__ == '__main__':
  23. main()

Client

  1. # example_client.py
  2. from pyeasyrpc.rpc import RPCClient
  3. def main():
  4. client = RPCClient("ServiceInstance")
  5. print("method_list", client.get_methods())
  6. print("add", client.add(1, 2))
  7. print("sub", client.sub(100, 1.1))
  8. print("make_dict", client.make_dict(a=1, b=2, c=3))
  9. print("make_list", client.make_list(1, [2], {3}))
  10. try:
  11. client.add()
  12. except Exception as ex:
  13. print(type(ex), ex)
  14. if __name__ == '__main__':
  15. main()

Async Client

  1. # example_client_async.py
  2. import asyncio
  3. from pyeasyrpc.rpc import AsyncRPCClient
  4. def main():
  5. client = AsyncRPCClient("ServiceInstance")
  6. print("method_list", client.get_methods())
  7. async def add():
  8. print("add", await client.add(1, 2))
  9. async def sub():
  10. print("sub", await client.sub(100, 1.1))
  11. async def make_dict():
  12. print("make_dict", await client.make_dict(a=1, b=2, c=3))
  13. async def make_list():
  14. print("make_list", await client.make_list(1, [2], {3}))
  15. async def catch_exception():
  16. try:
  17. await client.add()
  18. except Exception as ex:
  19. print(type(ex), ex)
  20. async def func():
  21. task = [add(), sub(), make_list(), make_dict(), catch_exception()]
  22. # task.extend([add() for i in range(100)])
  23. # task.extend([sub() for i in range(100)])
  24. # task.extend([make_list() for i in range(100)])
  25. # task.extend([make_dict() for i in range(100)])
  26. # task.extend([catch_exception() for i in range(100)])
  27. await asyncio.wait(task)
  28. loop = asyncio.get_event_loop()
  29. loop.run_until_complete(func())
  30. if __name__ == '__main__':
  31. main()