mprpc的包装器配置

mprpc-config的Python项目详细描述


mprpc_config

这是一个 mprpc 二次封装的包。

安装

pip install mprpc_config

用法

构建一个如下的目录结构

  • config.py 包含RPC配置属性(任意命名)

    INSTALLED_APP 必填参数,list 类型,将每一个包含 RPC 接口的模块名放进去

  • app_module_name RPC 接口模块(任意命名,可有多个)

    • implement.py 接口模块(任意命名,可有多个),里面包含 rpc 方法装饰的接口函数
  • server.py RPC 服务器

    1. 初始化 mprpc_config 的 Configration 类,将 config.py 的模块名 config 作为字符串传入
    2. 导入 mprpc_config.rpc_server 的 RPCServer 和 StreamServer,启动 RPC 服务器
    3. 使用 bind_class 方法将初始化后的所有 RPC 接口绑定给 RPCServer
    4. 使用 StreamServer 启动 RPC 服务器
  • client.py RPC 客户端(测试用,实际可分离)

    客户端启动方式可参考 mprpc

  • rpc_config RPC 服务配置模块,

像 Celery 一样使用。定义一个 RPC 接口只需在接口上加一个 @rpc 方法即可。

示例

示例代码在 example 目录

server.py

frommprpc_config.rpc_serverimportRPCServer,StreamServerfrommprpc_configimportrpc_configif__name__=='__main__':print('-------start server--------')config=rpc_config.Configuration("config")config.bind_class(RPCServer)server=StreamServer(('127.0.0.1',6000),RPCServer)server.serve_forever(stop_timeout=10)

client.py

frommprpc_config.rpc_clientimportRPCClient,RPCPoolClientfrommprpc_config.rpc_clientimportRPCPoolprint('---------- client ----------')client=RPCClient('127.0.0.1',6000)print('2 add 4: ',client.call('add',2,4))print('2 plus 4: ',client.call('plus',2,4))print('2 minus 4: ',client.call('minus',2,4))print('---------- done ----------')print('---------- client pool ----------')client_pool=RPCPool(RPCPoolClient,dict(host='127.0.0.1',port=6000))withclient_pool.connection()asclient:print('2 add 4: ',client.call('add',2,4))print('2 plus 4: ',client.call('plus',2,4))print('2 minus 4: ',client.call('minus',2,4))print('---------- done ----------')

启动 server 和 client

$ python server.py

---------- server ----------

$ python client.py

---------- client ----------
2 add 4:  62 plus 4:  82 minus 4:  -2
---------- done ----------
---------- client pool ----------
2 add 4:  62 plus 4:  82 minus 4:  -2
---------- done ----------

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java使用字符而不是字符串的意义   部分匹配长度字符串相似性的java正则表达式   java获取构造函数错误(错误:“(”或“[”)   java@Value注释无法正常工作SpringBoot   java hasKey在响应中没有看到字段   java JavaFX,没有JFXButtons和所有   在Log4j2中,是否可以根据键从记录器中筛选出某些键值对?   如何使用Java中Kafka的消息,从特定偏移量开始   java在单击后存储动态按钮的值   java Android编码:ViewRootImpl$CalledFromErrorThreadException。[Noob]   通过FileChooser保存pdf格式会提示在java中出现第二个对话框   用于空文件的java Spring集成文件标记   java我们应该如何编写get方法,以便私有字段不会超出其预期范围?   java Eclipse产品问题,Juno RequireHandle:org。日食e4。果心服务