Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/abersheeran/rpc.py
A fast and powerful RPC framework based on ASGI/WSGI.
https://github.com/abersheeran/rpc.py
annotation asgi cbor httpx json msgpack msgpack-rpc openapi-document pickle rpc rpc-framework wsgi
Last synced: 2 days ago
JSON representation
A fast and powerful RPC framework based on ASGI/WSGI.
- Host: GitHub
- URL: https://github.com/abersheeran/rpc.py
- Owner: abersheeran
- License: apache-2.0
- Created: 2020-07-11T04:22:24.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-07-06T00:33:14.000Z (4 months ago)
- Last Synced: 2024-10-14T13:52:51.348Z (20 days ago)
- Topics: annotation, asgi, cbor, httpx, json, msgpack, msgpack-rpc, openapi-document, pickle, rpc, rpc-framework, wsgi
- Language: Python
- Homepage:
- Size: 328 KB
- Stars: 192
- Watchers: 8
- Forks: 12
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# rpc.py
[![Codecov](https://img.shields.io/codecov/c/github/abersheeran/rpc.py?style=flat-square)](https://codecov.io/gh/abersheeran/rpc.py)
An fast and powerful RPC framework based on ASGI/WSGI. Based on WSGI/ASGI, you can deploy the rpc.py server to any server and use http2 to get better performance. And based on httpx's support for multiple http protocols, the client can also use http/1.0, http/1.1 or http2.
You can freely use ordinary functions and asynchronous functions for one-time response. You can also use generator functions or asynchronous generator functions to stream responses.
## Install
Install from PyPi:
```bash
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]# need use CBOR to serializer
pip install rpc.py[cbor]# or install all dependencies
pip install rpc.py[full]
```Install from github:
```bash
pip install git+https://github.com/abersheeran/[email protected]
```## Usage
### Server side:
Use
ASGI
mode to registerasync def
...```python
from typing import AsyncGenerator
from typing_extensions import TypedDictimport uvicorn
from rpcpy import RPCapp = 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) -> AsyncGenerator[int, None]:
for i in range(max_num):
yield iD = TypedDict("D", {"key": str, "other-key": str})
@app.register
async def query_dict(value: str) -> D:
return {"key": value, "other-key": value}if __name__ == "__main__":
uvicorn.run(app, interface="asgi3", port=65432)
```OR
Use
WSGI
mode to registerdef
...```python
from typing import Generator
from typing_extensions import TypedDictimport uvicorn
from rpcpy import RPCapp = 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) -> Generator[int, None, None]:
for i in range(max_num):
yield iD = TypedDict("D", {"key": str, "other-key": str})
@app.register
def query_dict(value: str) -> D:
return {"key": value, "other-key": value}if __name__ == "__main__":
uvicorn.run(app, interface="wsgi", port=65432)
```### Client side:
Notice: Regardless of whether the server uses the WSGI mode or the ASGI mode, the client can freely use the asynchronous or synchronous mode.
Use
httpx.Client()
mode to registerdef
...```python
from typing import Generator
from typing_extensions import TypedDictimport httpx
from rpcpy.client import Clientapp = 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) -> Generator[int, None, None]:
yieldD = TypedDict("D", {"key": str, "other-key": str})
@app.remote_call
def query_dict(value: str) -> D:
...
```OR
Use
httpx.AsyncClient()
mode to registerasync def
...```python
from typing import AsyncGenerator
from typing_extensions import TypedDictimport httpx
from rpcpy.client import Clientapp = 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) -> AsyncGenerator[int, None]:
yieldD = TypedDict("D", {"key": str, "other-key": str})
@app.remote_call
async def query_dict(value: str) -> D:
...
```### Server as client
You can also write two copies of code in one place. Just make sure that `server.register` is executed before `client.remote_call`.
```python
import httpx
from rpcpy import RPC
from rpcpy.client import Clientserver = RPC()
client = Client(httpx.Client(), base_url="http://127.0.0.1:65432/")@client.remote_call
@server.register
def sayhi(name: str) -> str:
return f"hi {name}"if __name__ == "__main__":
import uvicornuvicorn.run(app, interface="wsgi", port=65432)
```### Sub-route
If you need to deploy the rpc.py server under `example.com/sub-route/*`, you need to set `RPC(prefix="/sub-route/")` and modify the `Client(base_path=https://example.com/sub-route/)`.
### Serialization
Currently supports three serializers, JSON, Msgpack and CBOR. JSON is used by default. You can override the default `JSONSerializer` with parameters.
```python
from rpcpy.serializers import MsgpackSerializer, CBORSerializerRPC(
...,
response_serializer=MsgpackSerializer(),
)
# Or
Client(
...,
request_serializer=CBORSerializer(),
)
```## Type hint and OpenAPI Doc
Thanks to the great work of [pydantic](https://pydantic-docs.helpmanual.io/), which makes rpc.py allow you to use type annotation to annotate the types of function parameters and response values, and perform type verification and JSON serialization . At the same time, it is allowed to generate openapi documents for human reading.
### OpenAPI Documents
If you want to open the OpenAPI document, you need to initialize `RPC` like this `RPC(openapi={"title": "TITLE", "description": "DESCRIPTION", "version": "v1"})`.
Then, visit the `"{prefix}openapi-docs"` of RPC and you will be able to see the automatically generated OpenAPI documentation. (If you do not set the `prefix`, the `prefix` is `"/"`)
## Limitations
Currently, file upload is not supported, but you can do this by passing a `bytes` object.