Exemple #1
0
func initAndLock(initsigs bool) *Lock {
	// Lock the current goroutine to the current OS thread, until we have
	// released the GIL (as CPython uses per-thread state)
	runtime.LockOSThread()

	// Initialize the default Python interpreter
	if initsigs {
		C.Py_InitializeEx(1)
	} else {
		C.Py_InitializeEx(0)
	}

	// Enable Python thread support, and then immediately release the GIL (and
	// thus "deativate" and per-thread state associated with the current thread
	C.PyEval_InitThreads()
	C.PyEval_SaveThread()

	// We can now unlock the current goroutine from the current OS thread, as
	// there is no active per-thread state
	runtime.UnlockOSThread()

	// Now that Python is setup, we can return a locked Lock, ready for the
	// calling code to use
	return NewLock()
}
Exemple #2
0
// Initialize initializes the python interpreter and its GIL
func Initialize() error {
	// make sure the python interpreter has been initialized
	if C.Py_IsInitialized() == 0 {
		C.Py_Initialize()
	}
	if C.Py_IsInitialized() == 0 {
		return 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 {
		return fmt.Errorf("python: could not initialize the GIL")
	}

	return nil
}
Exemple #3
0
func threadInit() (defaultThreadState *C.PyThreadState) {
	initLock.Lock()
	defer initLock.Unlock()

	if !initialized {
		C.Py_InitializeEx(0)
		C.PyEval_InitThreads()
		C.PySys_SetArgvEx(0, nil, 0)

		pyEmptyTuple = C.PyTuple_New(0)
		falseObject = &object{C.False_INCREF()}
		trueObject = &object{C.True_INCREF()}

		defaultThreadState = C.PyEval_SaveThread()

		initialized = true
	}

	return
}
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)
}
Exemple #5
0
func InitThreads() {
	C.PyEval_InitThreads()
}