func (t *Thread) loop() { runtime.LockOSThread() threadState := threadInit() if threadState == nil { gilState := C.PyGILState_Ensure() oldThreadState := C.PyGILState_GetThisThreadState() threadState = C.PyThreadState_New(oldThreadState.interp) C.PyGILState_Release(gilState) } for f := range t.queue { C.PyEval_RestoreThread(threadState) f() threadState = C.PyEval_SaveThread() } gilState := C.PyGILState_Ensure() C.PyThreadState_Clear(threadState) C.PyThreadState_Delete(threadState) C.PyGILState_Release(gilState) }
func init() { log.Debug("> Initilize Python.") runtime.LockOSThread() if C.Py_IsInitialized() == 0 { C.Py_Initialize() } if C.Py_IsInitialized() == 0 { panic(fmt.Errorf("python: could not initialize the python interpreter")) } // make sure the GIL is correctly initialized if C.PyEval_ThreadsInitialized() == 0 { C.PyEval_InitThreads() } if C.PyEval_ThreadsInitialized() == 0 { panic(fmt.Errorf("python: could not initialize the GIL")) } log.Debug("< Initilized Python.") _tstate := C.PyGILState_GetThisThreadState() C.PyEval_ReleaseThread(_tstate) }