This is a description.
Project description
Goython
探索 Python 中调用使用 Go 实现的方法
TODO
- 交叉编译: github action go 多平台编译测试
- 调用测试:python 使用 subprocess 调用 go 生成的可执行文件
静态库
使用
Cython打包 Go 编译生成的库文件为静态库拓展
测试流程:
cd lib_static
make
动态库
使用
ctypes调用 Go 编译生成的动态库
测试流程:
cd lib_dynamic
make
gRPC
使用
Protocol Buffers定义接口,使用Go编写服务端,使用Python编写客户端
测试流程:
make test
Build
由于用户使用平台的多样性,以及用户可能并未安装 go 的开发环境,我们需要为用户预先编译好,为其提供可直接使用的二进制文件
需要适配的平台有:
- Linux:
x86_64,aarch64 - macOS:
x86_64,arm64 - Windows:
amd64
Platform 由操作系统 (GOOS) 和指令集架构 (GOARCH) 唯一确定
可选的 GOOS 有:
windowsdarwinlinux
可选的 GOARCH 有:
amd64&x86_64->amd64arm64&aarch64->arm64
💡 为不同平台分发可执行文件可以借助 Github Action 实现
Mock
模拟一个 SDK 的调用流程,进行 Python 与 Go 之间的交互
- 在 sdk 中启动 tcp server:
调用
sdk.init时使用subprocess.Popen启动服务并注册停止服务的相关hook - 在 sdk 中关闭 tcp server:
调用
sdk.finish时通过调用停止服务的hook向 sock 发送停止信号
注意,为了避免用户不执行
sdk.finish导致进程未被杀死,我们需要借助atexit在程序退出时停止服务进程
依赖
- protoc (如果需要重新编译
proto文件,需要安装) - Go with protobuf runtime and grpc tools
- Python with protobuf runtime and grpc tools
protoc
- Github Release 下载所需
protoc版本 - 解压后查看
README - 分别将
bin/*与include/*复制或移动至/usr/local/bin/与/usr/local/include/
grpc
quick start
思想:定义服务,指定可以远程调用的方法,并指定参数和响应
接口定义语言(IDL): Protocol Buffers
service HelloService {
rpc SayHello (HelloRequest) returns (HelloResponse);
}
message HelloRequest {
string greeting = 1;
}
message HelloResponse {
string reply = 1;
}
Service method
有以下四种可以定义的服务:
- Unary RPCs
- Server streaming RPCs: 服务端流式响应
- Client streaming RPCs: 客户端流式发送
- Bidirectional streaming RPCs: 双向流
rpc BidiHello(stream HelloRequest) returns (stream HelloResponse);
Difference between HTTP Restful API
借助 Restful API 实现端到端通信需要定义以下部分:
- 方法 GET/POST...
- 路由 /api/:foo/:bar
- 请求体 SearchParams/Body
- 响应体 Raw/Text/Json
gRPC 服务需要定义以下部分:
借助 ProtoBuf 定义:
- 服务 (service)
- 接口消息数据结构 (message)
借助
protoc生成供客户端与服务端使用的方法与数据结构
与 API 调用不同,借助生成的 stub,允许在服务端实现 ProtoBuf 中定义的服务,并在客户端直接通过同名方法进行调用。
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file service_grpc-0.0.2.tar.gz.
File metadata
- Download URL: service_grpc-0.0.2.tar.gz
- Upload date:
- Size: 7.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5ec4a6750b359e4d973da21aaecf26bd769001f48375284bf7654b68bcf97d94
|
|
| MD5 |
6c188e12fdabb10c6522370352b2763f
|
|
| BLAKE2b-256 |
9d519838b0deb729325e577cf1be661aed8c91683140b47823b068ee327cbb0f
|
File details
Details for the file service_grpc-0.0.2-py3-none-manylinux1_x86_64.whl.
File metadata
- Download URL: service_grpc-0.0.2-py3-none-manylinux1_x86_64.whl
- Upload date:
- Size: 7.3 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
920b94942b68a1aa30e032fc5f9bc5f589d0a37ee998c864ba0162d8eb8b0d5b
|
|
| MD5 |
bcaa217dd9c17494533aeda10f0d9c25
|
|
| BLAKE2b-256 |
0085cf19f6a8ee202eabadf59483635f102df73d9db0e0a776e735c4ab9e9521
|