一个基于ASGI/WSGI的快速而强大的RPC框架。
rpc.p的Python项目详细描述
在rpc.py在
一个基于ASGI/WSGI的快速而强大的RPC框架。在
基于WSGI/ASGI,您可以部署rpc.py服务器到任何服务器,并使用http2来获得更好的性能。在
安装
从PyPi安装:
pip install rpc.py # need use client pip install rpc.py[client]# need use pydantic type hint or OpenAPI docs pip install rpc.py[type]# need use msgpack to serializer pip install rpc.py[msgpack]# or install all dependencies pip install rpc.py[full]
从github安装:
^{pr2}$使用
服务器端:
<详细信息>ASGI
模式注册async def
…importuvicornfromrpcpyimportRPCapp=RPC(mode="ASGI")@app.registerasyncdefnone()->None:return@app.registerasyncdefsayhi(name:str)->str:returnf"hi {name}"@app.registerasyncdefyield_data(max_num:int):foriinrange(max_num):yieldiif__name__=="__main__":uvicorn.run(app,interface="asgi3",port=65432)详细信息>
或者
<详细信息>WSGI
模式注册def
…importuvicornfromrpcpyimportRPCapp=RPC()@app.registerdefnone()->None:return@app.registerdefsayhi(name:str)->str:returnf"hi {name}"@app.registerdefyield_data(max_num:int):foriinrange(max_num):yieldiif__name__=="__main__":uvicorn.run(app,interface="wsgi",port=65432)详细信息>
客户端:
注意:无论服务器使用WSGI模式还是ASGI模式,客户机都可以自由地使用异步或同步模式。在
<详细信息>httpx.Client()
模式注册def
…importhttpxfromrpcpy.clientimportClientapp=Client(httpx.Client(),base_url="http://127.0.0.1:65432/")@app.remote_calldefnone()->None:...@app.remote_calldefsayhi(name:str)->str:...@app.remote_calldefyield_data(max_num:int):yield详细信息>
或者
<详细信息>httpx.AsyncClient()
模式注册async def
…importhttpxfromrpcpy.clientimportClientapp=Client(httpx.AsyncClient(),base_url="http://127.0.0.1:65432/")@app.remote_callasyncdefnone()->None:...@app.remote_callasyncdefsayhi(name:str)->str:...@app.remote_callasyncdefyield_data(max_num:int):yield详细信息>
支线
如果需要部署rpc.py{{cd11>需要修改{cd11>下的设置}。在
序列化
目前支持三个序列化程序,JSON、Pickle和Msgpack。默认情况下使用JSON。您可以用参数覆盖默认的JSONSerializer
。在
importhttpxfromrpcpyimportRPCfromrpcpy.clientimportClientfromrpcpy.serializersimportPickleSerializer,MsgpackSerializerRPC(request_serializer=PickleSerializer(),response_serializer=MsgpackSerializer(),)Client(httpx.Client(),base_url="http://127.0.0.1:65432/",request_serializer=PickleSerializer(),response_serializer=MsgpackSerializer(),)
类型hint和OpenAPI Doc
多亏了pydantic的出色工作rpc.py允许您使用类型注释来注释函数参数和响应值的类型,并执行类型验证和JSON序列化。同时,它还可以生成供人类阅读的openapi文档。在
OpenAPI文档
如果您想打开OpenAPI文档,您需要初始化RPC
,如下所示RPC(openapi={"title": "TITLE", "description": "DESCRIPTION", "version": "v1"})
。在
然后,访问RPC的"{prefix}openapi-docs"
,您将能够看到自动生成的OpenAPI文档。(如果不设置prefix
,则prefix
为"/"
)
局限性
目前,不支持文件上载,但您可以通过传递bytes对象来完成此操作。在
- 项目
标签: