實現(xiàn)方法概述
因為go語言可以生成gcc可用的靜態(tài)庫,我們可以用go語言實現(xiàn)我們需要的功能,然后編譯成靜態(tài)庫,再用C語言調(diào)用該靜態(tài)庫,包裝成python3模塊,然后編譯成動態(tài)庫供python3調(diào)用。
實例
下面用一個用go線程亂序打印五次字符串參數(shù)的函數(shù)作為示例。
go程序
文件名:tryme.go
代碼:
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
import "C"
func runp(wg *sync.WaitGroup, str string) {
defer wg.Done()
t := rand.Intn(5)
time.Sleep(time.Second * time.Duration(t))
fmt.Println(str)
}
//export Tryme
func Tryme(str string) {
var wg sync.WaitGroup
for i := 1; i <= 5; i++ {
wg.Add(1)
go runp(&wg, fmt.Sprintf("%v %v", i, str))
}
wg.Wait()
}
func main() {}
編譯命令:
go build -o tryme.a -ldflags="-s -w" -buildmode=c-archive tryme.go
結(jié)果會生成2個文件:
- tryme.a
- tryme.h
C包裝程序
文件名:topy.c
代碼:
#include <Python.h>
#include "tryme.h"
#include "string.h"
void trythis(char *s){
GoString gs = {s,strlen(s)};
Tryme(gs);
}
//add wrapped function
static PyObject* wrap_trythis(PyObject *self, PyObject *args)
{
//Convert the python input objects into C objects
char *s;
if(!PyArg_ParseTuple(args, "s", &s)){
return NULL;
}
//Call c function
trythis(s);
//wrap the result from c function to python object.
return (PyObject*)Py_BuildValue("");
}
//define the PyMethodDef methods
static PyMethodDef wrap_methods[] ={
{"trythis", (PyCFunction)wrap_trythis, METH_VARARGS,NULL},
{NULL, NULL}
};
struct module_state {
PyObject *error;
};
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"topy",
NULL,
sizeof(struct module_state),
wrap_methods,
NULL,
NULL,
NULL,
NULL
};
//initilization function
PyMODINIT_FUNC PyInit_topy(void)
{
PyObject *module = PyModule_Create(&moduledef);
return module;
}
編譯命令:
gcc -c -I/usr/include/python3.6m -fpic topy.c
gcc -o topy.so -shared -lpthread topy.o tryme.a
結(jié)果生成動態(tài)庫: topy.so
用python3調(diào)用topy庫
代碼:
import topy
topy.trythis("hello boy.")
結(jié)果輸出(順序隨機):
4 hello boy.
5 hello boy.
2 hello boy.
1 hello boy.
3 hello boy.
結(jié)語
在網(wǎng)絡(luò)編程、多線程編程等方面,go語言有這極大的優(yōu)越性,非常適合用來擴展python3。