2012-09-15 23:19
浏览 142


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?

图片转代码服务由CSDN问答提供 功能建议

我目前使用Cython链接C和Python,并以慢速的python代码获得加速。 但是,我想使用goroutines来实现一个非常慢(并且非常可并行化)的代码,但是它必须可以从python调用。 (我已经看过这个问题 < p>如果有必要,我很乐意通过C(或Cython)来建立数据结构等,但是从错误修复/避免的角度来看,避免增加这一层是很好的。 \ n


  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • doujiao3016 2015-09-27 21:29

    Update 2015: possible as of 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.

    打赏 评论
  • dou29106 2012-09-16 01:39

    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.

    打赏 评论
  • duancan8382 2012-09-17 03:28

    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)

    打赏 评论
  • dougaimian1143 2017-02-01 05:52

    I've written an extension to setuptools which allows you to write cpython extensions that interface with go:

    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.

    打赏 评论

相关推荐 更多相似问题