Golang调⽤Python
Python是时髦的机器学习御⽤开发语⾔,Golang是⼤红⼤紫的新时代后端开发语⾔。Python很适合让搞算法的写写模型,⽽Golang很适合提供API服务,两位同志都红的发紫,这⾥就介绍⼀下正确搅基的办法
原理
Python提供了丰富的。⽽C和Go⼜可以通过cgo⽆缝集成。所以,直接通过Golang调⽤libpython,就可以实现Go调Python的功能了。确实没啥神奇,只要会⽤C调Python,马上就知道怎么⽤了。但问题是,如果有的选择,这个年代还有多少⼈愿意去裸写C和C++呢?诚⼼默念Golang⼤法好。
准备⼯作
Python :确保Python正确安装,所谓正确安装,就是在系统中能到libpython.so(dylib),到Python.h。⼀般linux直接安装python-devel,mac直接⽤homeb rew安装就可以。
Golang安装:Golang不需要什么特殊的处理,能到go即可。
安装libpython-go-binding
虽然直接⽤cgo调⽤libpython也不是不可以,但是有native-binding⽤起来肯定要爽的多。Github上有⼀个现成的Binding库。
go get github/sbinet/go-python
如果Python安装正确,这⾥会⾃动编译并显⽰提⽰,事就这样成了。
Have a try
⾸先写⼀个测试Python脚本
import numpy
import sklearn
a = 10
def b(xixi):
return xixi + "haha"
然后写⼀个Go脚本:
package main
import (
"github/sbinet/go-python"
"fmt"
)
func init() {
err := python.Initialize()
if err != nil {
panic(err.Error())
}
}
var PyStr = python.PyString_FromString
var GoStr = python.PyString_AS_STRING
func main() {
// import hello
InsertBeforeSysPath("/Users/vonng/anaconda2/lib/python2.7/site-packages")
hello := ImportModule("/Users/vonng/Dev/go/src/gitlab.alibaba-inc/cplus", "hello")
fmt.Printf("[MODULE] repr(hello) = %s\n", GoStr(hello.Repr()))
// print(hello.a)
a := hello.GetAttrString("a")
fmt.Printf("[VARS] a = %#v\n", python.PyInt_AsLong(a))
// print(hello.b)
b := hello.GetAttrString("b")
fmt.Printf("[FUNC] b = %#v\n", b)
// args = tuple("xixi",)
bArgs := python.PyTuple_New(1)
python.PyTuple_SetItem(bArgs, 0, PyStr("xixi"))
// b(*args)
res := b.Call(bArgs, python.Py_None)
fmt.Printf("[CALL] b('xixi') = %s\n", GoStr(res))
// sklearn
sklearn := hello.GetAttrString("sklearn")
skVersion := sklearn.GetAttrString("__version__")
fmt.Printf("[IMPORT] sklearn = %s\n", GoStr(sklearn.Repr()))
fmt.Printf("[IMPORT] sklearn version =  %s\n", GoStr(skVersion.Repr()))
}
// InsertBeforeSysPath will add given dir to python import path
func InsertBeforeSysPath(p string) string {
sysModule := python.PyImport_ImportModule("sys")
path := sysModule.GetAttrString("path")
python.PyList_Insert(path, 0, PyStr(p))
return GoStr(path.Repr())
}
// ImportModule will import python module from given directory
func ImportModule(dir, name string) *python.PyObject {
sysModule := python.PyImport_ImportModule("sys") // import sys
python printf输出格式
path := sysModule.GetAttrString("path")                    // path = sys.path
python.PyList_Insert(path, 0, PyStr(dir))                    // path.insert(0, dir)
return python.PyImport_ImportModule(name)            // return __import__(name)
}
打印输出为:
repr(hello) = <module 'hello' from '/Users/vonng/Dev/go/src/gitlab.alibaba-inc/cplus/hello.pyc'>
a = 10
b = &python.PyObject{ptr:(*python._C type_struct__object)(0xe90b1b8)}
b('xixi') = xixihaha
sklearn = <module 'sklearn' from '/Users/vonng/anaconda2/lib/python2.7/site-packages/sklearn/__init__.pyc'>
sklearn version =  '0.18.1'
这⾥简单解释⼀下。⾸先将这个脚本的路径添加到sys.path中。然后调⽤PyImport_ImportModule导⼊包
使⽤GetAttrString可以根据属性名获取对象的属性,相当于python中的.操作。调⽤Python函数可以采⽤Object.Call⽅法,,列表参数使⽤Tuple来构建。返回值⽤PyString_AS_STRING从Python字符串转换为C或Go的字符串。
更多⽤法可以参考。
但是只要有这⼏个API,就⾜够 Make python module rock & roll。充分利⽤Golang和Python各⾃的特性,构建灵活⽽强⼤的应⽤了。