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.1.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

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

service_grpc-0.0.1-py3-none-manylinux1_x86_64.whl (9.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: service_grpc-0.0.1.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • 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.1.tar.gz
Algorithm Hash digest
SHA256 d3ebba23255637fda6136ec127eee6a83038af34369851b5998f5c70972d9433
MD5 04586c1151cfaf6b18bc228c789088f0
BLAKE2b-256 4f1ff3ae58ec5330a84a97708b0f09c05a9475828f266f7286eb4fb192b3658c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for service_grpc-0.0.1-py3-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e4d376745b64db0c1e1fb552160e88c029410f79b937fb8e677a456d1fbe6a97
MD5 63903351ab419e1ca95d0b7173fb6670
BLAKE2b-256 9a0a72809a246e383a6f86e0a83ac5316441d3e9cd0d4c425f7aeafad5a9477a

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