a rpc framework for python base on grpc
Project description
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
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
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__)
客户端输出
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}
Project details
Release history Release notifications | RSS feed
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.2.tar.gz
(22.1 kB
view details)
Built Distribution
tzrpc-0.0.2-py3-none-any.whl
(29.8 kB
view details)
File details
Details for the file tzrpc-0.0.2.tar.gz
.
File metadata
- Download URL: tzrpc-0.0.2.tar.gz
- Upload date:
- Size: 22.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef8c18d0e18cd79f45a96fe2e2a25dae862c3e957d7d71edb5fb448e44548d77 |
|
MD5 | 45d407a26060c40496095a6a494d9c8e |
|
BLAKE2b-256 | dac106c5dd1cb04a981c73890b4b2cbca8d997b6a7ff7afa21de4a3d11dd25b9 |
File details
Details for the file tzrpc-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: tzrpc-0.0.2-py3-none-any.whl
- Upload date:
- Size: 29.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fbeba50a883ea9e7c2082b9f53b7dd4d1f64dfad0b8014f7da72931fcda7561b |
|
MD5 | 2be8f9676c4e4d5a25e2b71b046d37b0 |
|
BLAKE2b-256 | 70d702c3fed74d4d61f9b03cf8307371b9cf488f3e2fa47feed7fddda0af39da |