Skip to main content

a rpc framework for python base on grpc

Project description


TZRPC

让深度学习调用: 简单!!! 高效!!!


versions GitHub repo size Contributions welcome GitHub forks GitHub Repo stars license

深度学习模型在落地时需要提供高效快速交互接口,业务逻辑和深度模型解码通常运行在不同类型的机器上。 Http 并不适合大量数据的交互,而RPC (Remote Procedure Call) 远程过程调用, 而RPC在TCP层实现。提高了开发效率,算法工程师可以不必花费更多精力放在具体的接口实现上,而是专注于算法优化上。

tzrpc 框架基于google的 grpc 实现,需要Python 3.7及以上, 支持流式传输!!!

目前支持以下基础类型:

TZRPC 类型 python 类型 是否支持
String str
Integer int
Float float
Double float
Boolean bool
Bytes bytes
Numpy numpy
Tensor torch.Tensor
使用pickle模块反序列化 任意python对象

快速使用

安装

pip install tzrpc

或拉取最新代码,安装

git clone https://github.com/lovemefan/TZRPC.git
cd TZRPC
pip install -e .

服务端 Server.py

from tzrpc import TZRPC_Server
import numbers
server = TZRPC_Server(__name__)


@server.register
def say_hello(text):
    return "hello world " + text

@server.register
def send_numpy_obj(data):
    return data * 2 + 1

@server.register
def send_torch_tensor_obj(data):
    return data @ data.T

@server.register
def send_bytes(data: bytes):
    return data + data

@server.register
def send_number(data: numbers.Number):
    return data * 2


@server.register
def send_bool(_bool: bool):
    return not _bool


@server.register
def send_python_obj(data):
    return data

@server.register(stream=True)
def gumbel(num):
    if num % 3 == 0:
        yield f"number is {num}, you win"

if __name__ == '__main__':
    server.run("localhost", 8000)

客户端 Client.py

from tzrpc import TZPRC_Client
import numpy as np
import torch
import numbers
SERVER_ADDRESS = "localhost:8000"
client = TZPRC_Client(SERVER_ADDRESS)


@client.register
def say_hello(text):
    return text

@client.register
def send_numpy_obj():
    data = np.array([[1, 2, 3], [4, 5, 6]])
    return data

@client.register
def send_torch_tensor_obj():
    data = torch.tensor([[1, 2, 3], [4, 5, 6]])
    return data

@client.register
def send_bytes():
    return b"just for test"

@client.register
def send_number(data: numbers.Number):
    return data


@client.register
def send_bool(_bool: bool):
    return _bool

@client.register
def send_python_obj(data):
    return data

@client.register(stream=True)
def gumbel(num):
    for i in range(num):
        yield i

if __name__ == '__main__':
    print(say_hello("lovemefan"))
    print(send_numpy_obj())
    print(send_torch_tensor_obj())
    print(send_bytes())
    print(send_number(2))
    print(send_number(1/3))
    print(send_bool(True))
    print(send_bool(False))

    class testOb:
        def __init__(self, name, age):
            self.name = name
            self.age = age

    python_obj = testOb("test_name", 20)
    print(send_python_obj(python_obj).__dict__)

    # 流式demo
    for i in gumbel(10):
        print(i)

客户端输出

hello world lovemefan

[[ 3  5  7]
 [ 9 11 13]]

tensor([[14, 32],
        [32, 77]])

b'just for testjust for test'

4

0.6666666666666666

False

True

{'name': 'test_name', 'age': 20}

number is 0, you win
number is 3, you win
number is 6, you win
number is 9, you win

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tzrpc-0.0.4.tar.gz (23.5 kB view details)

Uploaded Source

Built Distribution

tzrpc-0.0.4-py3-none-any.whl (31.5 kB view details)

Uploaded Python 3

File details

Details for the file tzrpc-0.0.4.tar.gz.

File metadata

  • Download URL: tzrpc-0.0.4.tar.gz
  • Upload date:
  • Size: 23.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for tzrpc-0.0.4.tar.gz
Algorithm Hash digest
SHA256 61eda27d079acb4c923d46fe87b50d0aaa0de67cc7cd09d8e4b05d14cbce2606
MD5 cb6722bae480271ffb6687b4f51ba17f
BLAKE2b-256 49dc701a345b9ee52bc39ea596c0e7d7e8df3f6dd025c264f1ce0ae96b28dbd9

See more details on using hashes here.

File details

Details for the file tzrpc-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: tzrpc-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 31.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for tzrpc-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ca6fee141e9d7ffab9635f5451a68335ebcb3c519392f5c0e50a634c24b1032f
MD5 2f3115736fc5a7000697008fb7656e1e
BLAKE2b-256 ccc90ee3b79659b79b35bcff01317112b101711162c6e4388469e4d3321903f1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page