-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
147 lines (112 loc) · 3.04 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
/*
#cgo pkg-config: python-2.7
#include "Python.h"
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
extern void createThreadCallback();
static PyObject* thread_callback() {
PyObject *_module_name, *_module;
PyGILState_STATE _gstate;
// Initialize python GIL state
_gstate = PyGILState_Ensure();
// Now execute some python code (call python functions)
_module_name = PyString_FromString("json_dump");
_module = PyImport_Import(_module_name);
// Call a method of the class with no parameters
PyObject *_attr, *_result;
_attr = PyObject_GetAttr(_module, PyString_FromString("run"));
_result = PyObject_CallObject(_attr, NULL);
// Clean up
Py_DECREF(_module);
Py_DECREF(_module_name);
Py_DECREF(_attr);
PyGILState_Release(_gstate);
return _result;
}
static void createThread(pthread_t* pid) {
pthread_create(pid, NULL, (void*)createThreadCallback, pid);
}
static void initialize_python () {
if (Py_IsInitialized() == 0) {
Py_Initialize();
//fprintf(stdout, "> Py_Initialize\n");
}
// make sure the GIL is correctly initialized
if (PyEval_ThreadsInitialized() == 0) {
PyEval_InitThreads();
//fprintf(stdout, "> PyEval_ThreadsInitialized\n");
}
PyEval_ReleaseThread(PyGILState_GetThisThreadState());
}
*/
import "C"
import (
"encoding/json"
"fmt"
"github.com/op/go-logging"
"sync"
//"time"
//"runtime"
)
var callbacks map[*C.pthread_t]ThreadCallback
type ThreadCallback func(a *C.PyObject)
var lock sync.Mutex
//export createThreadCallback
func createThreadCallback(pid *C.pthread_t) {
lock.Lock()
defer lock.Unlock()
_cb, _ok := callbacks[pid]
if !_ok {
panic(fmt.Errorf("failed to found thread callback for `%v`", pid))
}
//runtime.LockOSThread()
_result := C.thread_callback()
_cb(_result)
//defer runtime.UnlockOSThread()
defer func() {
delete(callbacks, pid)
}()
}
func create_thread(cb ThreadCallback) {
_pid := new(C.pthread_t)
log.Debug("> create_thread: %v", _pid)
callbacks[_pid] = cb
C.createThread(_pid)
log.Debug("< create_thread: %v", _pid)
}
var log = logging.MustGetLogger("gopython-test")
func init() {
logging.SetLevel(logging.INFO, "gopython-test")
logging.SetLevel(logging.DEBUG, "gopython-test")
log.Debug("> Initilize Python.")
callbacks = map[*C.pthread_t]ThreadCallback{}
C.initialize_python()
}
func main() {
log.Info("python embedding test in golang, using `pthreads`.")
n := 400
var _wg sync.WaitGroup
_wg.Add(n)
for i := 0; i < n; i++ {
go create_thread(func(result *C.PyObject) {
defer _wg.Done()
_result_string := C.GoString(C.PyString_AsString(result))
log.Debug("< got result string: %v (%T)", _result_string, _result_string)
var _parsed []interface{}
if _err := json.Unmarshal([]byte(_result_string), &_parsed); _err != nil {
panic(fmt.Errorf("got invalid result from python function, `%v`", _result_string))
}
log.Debug(
"< got thread_id=%v\tnow=%s",
int64(_parsed[0].(float64)),
_parsed[1].(string),
)
})
}
_wg.Wait()
}