rpyc python rpc 框架

rpyc 属于一个老牌项目了,支持rpc 模式的处理,同时支持面向服务的开发模式,包含了一些安全控制,支持服务注册,服务发现

包含的特性

  • 透明,可以使用类似本地模式的方法访问
  • 对称,client 以及server 都可以支持调用
  • 支持同步以及异步操作
  • 平台无关
  • 低开销
  • 安全,支持基于tls,以及ssh 的访问模式
  • zero deploy 支持(基于plumbum)
  • 支持面向服务的开发模式,支持服务发现

参考玩法

  • 启动注册中心
./venv/bin/rpyc_registry  -l enable
  • server 服务
import rpyc
from rpyc import Service
from rpyc.utils.server import ThreadedServer
@rpyc.service
class MyService(Service):
    # 定义服务名称
    ALIASES = ["MyService"]
    def on_connect(self, conn):
        print("Connected")
 
    def on_disconnect(self, conn):
        print("Disconnected")
    @rpyc.exposed
    def get_answer(self):
        return 42
    @rpyc.exposed
    def get_question(self):
        return "what is the answer to everything?"
server = ThreadedServer(MyService,port=18861,auto_register=True)
server.start()
  • client
import rpyc
list_service = rpyc.list_services()
# 基于服务名称的链接,如果是多个服务会选择第一个
info = rpyc.connect_by_service("MyService").root.get_answer()
print(info)

说明

rpyc 属于一个比较老的项目了,当然注意使用,以及安全问题(推荐开启ssl 支持)

参考资料

https://rpyc.readthedocs.io/en/latest/index.html
https://github.com/tomerfiliba-org/rpyc
https://rpyc.readthedocs.io/en/latest/docs/zerodeploy.html#zerodeploy

posted on   荣锋亮  阅读(91)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2020-09-29 QRGen 一个灵活简单的java 二维码生成jar 包
2020-09-29 token模式请求图片资源
2020-09-29 扩展自定义spring boot prometheus metrics
2020-09-29 Managing PostgreSQL users and roles
2019-09-29 Cortex Architecture
2019-09-29 cortex 水平扩展试用
2019-09-29 cortex 基本试用

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示