TZRPC
让深度学习调用: 简单!!! 高效!!!
深度学习模型在落地时需要提供高效快速交互接口,业务逻辑和深度模型解码通常运行在不同类型的机器上。 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
Metadata
Release files for tzrpc 0.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tzrpc-0.0.4.tar.gz | 23.5 kB | Without provenance | Details for tzrpc-0.0.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tzrpc-0.0.4-py3-none-any.whl | Python 3 | none | any | Details for tzrpc-0.0.4-py3-none-any.whl |
Total release size: 55.0 kB
Release files / tzrpc-0.0.4.tar.gz
| Download URL | tzrpc-0.0.4.tar.gz |
|---|---|
| Size | 23.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
61eda27d079acb4c923d46fe87b50d0aaa0de67cc7cd09d8e4b05d14cbce2606
|
|
BLAKE2b-256 checksum How to use checksums |
49dc701a345b9ee52bc39ea596c0e7d7e8df3f6dd025c264f1ce0ae96b28dbd9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.18
|
Release files / tzrpc-0.0.4-py3-none-any.whl
| Download URL | tzrpc-0.0.4-py3-none-any.whl |
|---|---|
| Size | 31.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ca6fee141e9d7ffab9635f5451a68335ebcb3c519392f5c0e50a634c24b1032f
|
|
BLAKE2b-256 checksum How to use checksums |
ccc90ee3b79659b79b35bcff01317112b101711162c6e4388469e4d3321903f1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.18
|
Security for tzrpc 0.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.