Skip to main content

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 的开发环境,我们需要为用户预先编译好,为其提供可直接使用的二进制文件

需要适配的平台有:

  1. Linux: x86_64, aarch64
  2. macOS: x86_64, arm64
  3. Windows: amd64

Platform 由操作系统 (GOOS) 和指令集架构 (GOARCH) 唯一确定

可选的 GOOS 有:

  1. windows
  2. darwin
  3. linux

可选的 GOARCH 有:

  1. amd64 & x86_64 -> amd64
  2. arm64 & aarch64 -> arm64

💡 为不同平台分发可执行文件可以借助 Github Action 实现

Mock

模拟一个 SDK 的调用流程,进行 Python 与 Go 之间的交互

  1. 在 sdk 中启动 tcp server: 调用 sdk.init 时使用 subprocess.Popen 启动服务并注册停止服务的相关 hook
  2. 在 sdk 中关闭 tcp server: 调用 sdk.finish 时通过调用停止服务的 hook 向 sock 发送停止信号

注意,为了避免用户不执行 sdk.finish 导致进程未被杀死,我们需要借助 atexit 在程序退出时停止服务进程

依赖

  1. protoc (如果需要重新编译 proto 文件,需要安装)
  2. Go with protobuf runtime and grpc tools
  3. Python with protobuf runtime and grpc tools

protoc

  1. Github Release 下载所需 protoc 版本
  2. 解压后查看 README
  3. 分别将 bin/*include/* 复制或移动至 /usr/local/bin//usr/local/include/

grpc

quick start

  1. Go Quick Start
  2. Python Quick Start

思想:定义服务,指定可以远程调用的方法,并指定参数和响应

接口定义语言(IDL): Protocol Buffers

service HelloService {
  rpc SayHello (HelloRequest) returns (HelloResponse);
}

message HelloRequest {
  string greeting = 1;
}

message HelloResponse {
  string reply = 1;
}

Service method

有以下四种可以定义的服务:

  1. Unary RPCs
  2. Server streaming RPCs: 服务端流式响应
  3. Client streaming RPCs: 客户端流式发送
  4. Bidirectional streaming RPCs: 双向流
rpc BidiHello(stream HelloRequest) returns (stream HelloResponse);

Difference between HTTP Restful API

借助 Restful API 实现端到端通信需要定义以下部分:

  1. 方法 GET/POST...
  2. 路由 /api/:foo/:bar
  3. 请求体 SearchParams/Body
  4. 响应体 Raw/Text/Json

gRPC 服务需要定义以下部分:

借助 ProtoBuf 定义:

  1. 服务 (service)
  2. 接口消息数据结构 (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

service_grpc-0.0.3.tar.gz (7.2 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

service_grpc-0.0.3-py3-none-manylinux1_x86_64.whl (7.3 MB view details)

Uploaded Python 3

service_grpc-0.0.3-py3-none-macosx_11_0_arm64.whl (6.9 MB view details)

Uploaded Python 3macOS 11.0+ ARM64

File details

Details for the file service_grpc-0.0.3.tar.gz.

File metadata

  • Download URL: service_grpc-0.0.3.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

Hashes for service_grpc-0.0.3.tar.gz
Algorithm Hash digest
SHA256 1563390b57ffa1c679a2d8bc804c5658d6adf81a71adeb2aa8b47670b797229a
MD5 70781682c1c9f5f9eeea3c85425e453e
BLAKE2b-256 7b03826a0c5841bce6c875f79814001ae33183d1722e1942611f68f6d58f7212

See more details on using hashes here.

File details

Details for the file service_grpc-0.0.3-py3-none-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for service_grpc-0.0.3-py3-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9a1f69126e40aa800d5fa33d7b1bbc15396d8752da85052c8572ae95897869a0
MD5 9f7fe2ec72dcf2db4e57baed5ec124a7
BLAKE2b-256 9868b3101ea5e4f90d3f15a4a03c2d1de40885c2bdd0e393dcb876e6d98e26b7

See more details on using hashes here.

File details

Details for the file service_grpc-0.0.3-py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for service_grpc-0.0.3-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 73f437e1aac7373cbb6ddebc83ce37fcdfeee5e9593494a501c3570e781b61f6
MD5 59a2eaa046e1e1a70d132f143475633a
BLAKE2b-256 16a6f553a5974356ea242c7b7ff72c5a502a82a679f2a3eeb3fa2c7b17547503

See more details on using hashes here.

Supported by

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