V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
abersheeran
V2EX  ›  Python

基于 WSGI/ASGI 双协议的 rpc 框架

  •  
  •   abersheeran ·
    abersheeran · 2020-07-19 17:00:02 +08:00 · 1387 次点击
    这是一个创建于 1370 天前的主题,其中的信息可能已经有所发展或是发生改变。

    整了一个 rpc 框架,支持普通函数、生成器函数以及它们的异步版本。

    https://github.com/abersheeran/rpc.py

    感觉好像不缺什么了?欢迎大家来用。

    Server side:

    import uvicorn
    from rpcpy import RPC
    
    app = RPC(mode="ASGI")
    
    
    @app.register
    async def none() -> None:
        return
    
    
    @app.register
    async def sayhi(name: str) -> str:
        return f"hi {name}"
    
    
    @app.register
    async def yield_data(max_num: int):
        for i in range(max_num):
            yield i
    
    
    if __name__ == "__main__":
        uvicorn.run(app, interface="asgi3", port=65432)
    

    OR

    import uvicorn
    from rpcpy import RPC
    
    app = RPC()
    
    
    @app.register
    def none() -> None:
        return
    
    
    @app.register
    def sayhi(name: str) -> str:
        return f"hi {name}"
    
    
    @app.register
    def yield_data(max_num: int):
        for i in range(max_num):
            yield i
    
    
    if __name__ == "__main__":
        uvicorn.run(app, interface="wsgi", port=65432)
    

    Client side:

    import httpx
    from rpcpy.client import Client
    
    app = Client( httpx.Client(), base_url="http://127.0.0.1:65432/")
    
    
    @app.remote_call
    def none() -> None:
        ...
    
    
    @app.remote_call
    def sayhi(name: str) -> str:
        ...
    
    
    @app.remote_call
    def yield_data(max_num: int):
        yield 
    

    OR

    import httpx
    from rpcpy.client import Client
    
    app = Client( httpx.AsyncClient(), base_url="http://127.0.0.1:65432/")
    
    
    @app.remote_call
    async def none() -> None:
        ...
    
    
    @app.remote_call
    async def sayhi(name: str) -> str:
        ...
    
    
    @app.remote_call
    async def yield_data(max_num: int):
        yield
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1096 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 23:32 · PVG 07:32 · LAX 16:32 · JFK 19:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.