• Stars
    star
    189
  • Rank 204,649 (Top 5 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created over 4 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A fast and powerful RPC framework based on ASGI/WSGI.

rpc.py

Codecov

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:

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:

pip install git+https://github.com/abersheeran/[email protected]

Usage

Server side:

Use ASGI mode to register async def...
from typing import AsyncGenerator
from typing_extensions import TypedDict

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) -> AsyncGenerator[int, None]:
    for i in range(max_num):
        yield i


D = 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 register def...
from typing import Generator
from typing_extensions import TypedDict

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) -> Generator[int, None, None]:
    for i in range(max_num):
        yield i


D = 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 register def...
from typing import Generator
from typing_extensions import TypedDict

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) -> Generator[int, None, None]:
    yield


D = TypedDict("D", {"key": str, "other-key": str})


@app.remote_call
def query_dict(value: str) -> D:
    ...

OR

Use httpx.AsyncClient() mode to register async def...
from typing import AsyncGenerator
from typing_extensions import TypedDict

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) -> AsyncGenerator[int, None]:
    yield


D = 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.

import httpx
from rpcpy import RPC
from rpcpy.client import Client

server = 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 uvicorn

    uvicorn.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, Pickle, Msgpack and CBOR. JSON is used by default. You can override the default JSONSerializer with parameters.

from rpcpy.serializers import PickleSerializer, MsgpackSerializer, CBORSerializer

RPC(
    ...,
    response_serializer=MsgpackSerializer(),
)
# Or
Client(
    ...,
    request_serializer=PickleSerializer(),
)

Type hint and OpenAPI Doc

Thanks to the great work of pydantic, 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.

More Repositories

1

asgi-ratelimit

A ASGI Middleware to rate limit
Python
291
star
2

kui

An easy-to-use web framework. Supports both WSGI and ASGI modes. Gevent or asyncio, this is the question.
Python
279
star
3

a2wsgi

Convert WSGI app to ASGI app or ASGI app to WSGI app.
Python
183
star
4

cool

Make Python code cooler. Less is more.
Python
138
star
5

baize

Powerful and exquisite WSGI/ASGI framework/toolkit.
Python
75
star
6

websocks

A proxy server base on websocket
Python
72
star
7

poetry2setup

Convert python-poetry(pyproject.toml) to setup.py.
Python
57
star
8

r2-webdav

Use Cloudflare Workers to provide a WebDav interface for Cloudflare R2.
TypeScript
56
star
9

mingshe

A better Python. It is also a template for you to create a superset of Python.
Python
51
star
10

sync-gitee-mirror

使用 GitHub actions 同步 GitHub 仓库到 Gitee 仓库
16
star
11

china-region-data

中国行政区域数据
Python
14
star
12

pdm-shell

Use `pdm shell` set PATH and PYTHONPATH in the current shell
Python
13
star
13

pep249

Provide minimum implementation check and connection pool of PEP249.
Python
12
star
14

mywxmp

我的微信公众号 ”aber的个人号“
Python
12
star
15

zibai

A modern high-performance pure-Python WSGI server.
Python
11
star
16

setup.py

Poetry-based pypi package template
Python
9
star
17

vless-ws

A cloudflare worker that implements the vless server
TypeScript
8
star
18

runweb

Run web server with one command.
Python
7
star
19

pdm-version

Make `pdm version` like `poetry version`
Python
7
star
20

telegram-bot

Python
7
star
21

coolsql

Makes it easier to write raw SQL in Python.
Python
6
star
22

Ahnu

安徽师范大学相关程序
Python
6
star
23

clash-to-v2rayN

转换 Clash 订阅格式到 v2rayN 订阅格式
TypeScript
6
star
24

rgo-error

Like Rust's `Result` type, but for Go.
Go
5
star
25

localtasks

Google Cloud Tasks Queue substitute in local
Python
5
star
26

asgi-benchmark

Performance comparison between ASGI frameworks
Python
4
star
27

aligi

为阿里无服务函数提供API网关的协议解析
Python
4
star
28

tools-in-web

TypeScript
3
star
29

asynchronous

Efficiently convert synchronous code to asynchronous.
Python
3
star
30

BingImageCreator

High quality image generation by Microsoft. Reverse engineered API.
Python
3
star
31

empty-pypi

empty pypi package
2
star
32

http2tcp

Convert HTTP request to encrypted TCP channel
Go
2
star
33

httpbenchmark

A programmable HTTP stress testing tool. Written in Golang.
Go
2
star
34

mkdocs-version

Simple and easy-to-use mkdocs version plugin based on git tags
Python
2
star
35

qq-group-bot

基于 QQ BOT 平台 API 的群聊机器人
Python
2
star
36

compose.yaml

1
star
37

issue

Filter logs by name. Allow Unix shell style wildcards.
Python
1
star
38

rust-h11

A pure-Rust, bring-your-own-I/O implementation of HTTP/1.1
Rust
1
star
39

abersheeran

1
star
40

blog-comments

Comments for my blog
1
star
41

typeddict

Use `TypedDict` replace pydantic definitions.
Python
1
star
42

kjango

Use kui with django orm/admin/or other anything.
1
star