Go 和 Python 之间紧密的双向集成
Call Python 3 functions and methods from within your Go program while exposing Go functions and methods to Python.
This is not an implementation of Python in Go. Rather, py4go works by embedding the CPython runtime into your Go program using cgo functionality.
The expected use cases are not low-latency integration, but rather tight bidirectional integration. You can combine the full Go ecosystem with the full Python ecosystem.
Though you can achieve some integration by using Go's exec
package to run python, with py4go you get fine-grained access to individual functions, objects,
methods, and variables.
To get started try running scripts/example. Note that you need the Python
development libraries installed. E.g. in Fedora:
sudo dnf install python3-devel…Calling Python code from Go is relatively straightforward because Python is a dynamic language and CPython is an interpreted runtime. Exposing Go code to Python is more involved as it requires writing wrapper functions in C, which we omitted in the example above. See the examples directory for more detail.
In the future we are hoping to make this easier, perhaps via a C code generator based on static analysis of function signatures and types.
There are several issues to be aware of:
pkg-config: python3-embed to
locate the CPython SDK, which works on Fedora-based operating systems. But, because where you
build will determine the requirements for where you will run, it may be best to build on
Fedora, either directly or in a virtual machine or container. Unfortunately cgo does not let us
parameterize that pkg-config directive, thus you will have to modify our source files in order to
build on/for other operating systems.Release on all Python references to ensure that they are
garbage collected. Luckily, the defer keyword makes this easy enough in many circumstances.python.SaveThreadState and
python.EnsureGilState as appropriate. See the examples for more detail.暂无开放 Issues,或尚未同步最近议题。