使用Go(Golang)编写Python扩展

huangapple go评论110阅读模式
英文:

Writing a Python extension in Go (Golang)

问题

我目前使用Cython来链接C和Python,并在Python代码的慢部分中获得加速。然而,我想使用goroutines来实现一个非常慢(且非常可并行化)的代码片段,但它必须可以从Python中调用。(我已经看过这个问题)

如果必要的话,我可以通过C(或Cython)来设置数据结构等,但从错误修复/避免的角度来看,避免这个额外的层会很好。

在不需要重新发明任何轮子的情况下,最简单的方法是什么?

英文:

I currently use Cython to link C and Python, and get speedup in slow bits of python code. However, I'd like to use goroutines to implement a really slow (and very parallelizable) bit of code, but it must be callable from python. (I've already seen this question)

I'm (sort of) happy to go via C (or Cython) to set up data structures etc if necessary, but avoiding this extra layer would be good from a bug fix/avoidance point of view.

What is the simplest way to do this without having to reinvent any wheels?

答案1

得分: 47

2015年更新:从Go 1.5开始可以实现 https://blog.filippo.io/building-python-modules-with-go-1-5/

> 使用Go 1.5,您可以构建.so对象并将其作为Python模块导入,直接从Python中运行Go代码(而不是C代码)。

另请参阅https://github.com/go-python/gopy

> gopy可以从Go包生成一个CPython扩展模块。

英文:

Update 2015: possible as of Go 1.5 https://blog.filippo.io/building-python-modules-with-go-1-5/

> with Go 1.5 you can build .so objects and import them as Python modules, running Go code (instead of C) directly from Python.

See also https://github.com/go-python/gopy

> gopy generates a CPython extension module from a go package.

答案2

得分: 10

很遗憾,目前还不可能实现。Go可以运行C代码(而且该C代码可以回调到Go),但是main函数必须是在Go中,这样Go运行时才能设置好一切。

英文:

Unfortunately, this is not currently possible. Go can run C code (and that C code can then call back into Go), but the main function has to be in Go, so the Go runtime can set things up.

答案3

得分: 3

我写了一个扩展来扩展setuptools,它允许你编写与go接口的cpython扩展:https://github.com/asottile/setuptools-golang

这里有一些示例扩展:

有趣的是,这些扩展可以像任何其他pip包一样安装,并支持cpython和pypy。

PEP 513 manylinux1 wheels也可以通过setuptools-golang-build-manylinux-wheels工具构建以提供预构建的wheels。

这种方法与@ColonelPanic的答案几乎相同,但使用了一些额外的技巧来实现python2和python3的兼容性。

英文:

I've written an extension to setuptools which allows you to write cpython extensions that interface with go: https://github.com/asottile/setuptools-golang

There's a couple example extensions here:

The neat thing is these can be installed just like any other pip package and support both cpython and pypy.

PEP 513 manylinux1 wheels can also be built to provide pre-built wheels via the setuptools-golang-build-manylinux-wheels tool.

The approach is nearly identical to the one in @ColonelPanic's answer but uses some additional tricks to enable python2 + python3 compatibility.

答案4

得分: 2

有一个go-python包专门用于帮助您在Go中编写Python扩展:

这个包提供了一个可执行文件"go-python",它只是加载"python"然后调用python.Py_Main(os.Args)。这样做的理由是,在这样的可执行文件下,基于Go的C-Python扩展将更容易实现(因为这通常意味着通过一些相当复杂的函数跳转从C调用到Go)。

英文:

There is a go-python package precisely to help you write Python extensions in Go:

> this package provides an executable "go-python" which just loads
> "python" and then call python.Py_Main(os.Args). the rational being
> that under such an executable, go based extensions for C-Python would
> be easier to implement (as this usually means calling into go from C
> through some rather convoluted functions hops)

huangapple
  • 本文由 发表于 2012年9月16日 07:19:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/12443203.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定