Skip to content

Commit 7632738

Browse files
committed
Fixes #563
1 parent d6f3357 commit 7632738

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

org.lflang/src/lib/Python/pythontarget.c

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,15 @@ static PyObject* py_request_stop(PyObject *self) {
254254
static PyObject* py_main(PyObject *self, PyObject *args) {
255255
DEBUG_PRINT("Initializing main.");
256256
const char *argv[] = {TOSTRING(MODULE_NAME), NULL };
257+
258+
// Initialize the Python interpreter
259+
Py_Initialize();
260+
261+
DEBUG_PRINT("Initialized the Python interpreter.");
262+
263+
Py_BEGIN_ALLOW_THREADS
257264
lf_reactor_c_main(1, argv);
265+
Py_END_ALLOW_THREADS
258266

259267
Py_INCREF(Py_None);
260268
return Py_None;
@@ -966,25 +974,17 @@ PyObject* convert_C_action_to_py(void* action) {
966974
*/
967975
PyObject*
968976
get_python_function(string module, string class, int instance_id, string func) {
969-
970-
// Set if the interpreter is already initialized
971-
int is_initialized = 0;
972-
973-
if (Py_IsInitialized()) {
974-
is_initialized = 1;
975-
}
976-
977977
DEBUG_PRINT("Starting the function start().");
978978

979979
// Necessary PyObject variables to load the react() function from test.py
980980
PyObject *pFileName, *pModule, *pDict, *pClasses, *pClass, *pFunc;
981981

982982
PyObject *rValue;
983983

984-
// Initialize the Python interpreter
985-
Py_Initialize();
986984

987-
DEBUG_PRINT("Initialized the Python interpreter.");
985+
986+
PyGILState_STATE gstate;
987+
gstate = PyGILState_Ensure();
988988

989989
// If the Python module is already loaded, skip this.
990990
if (globalPythonModule == NULL) {
@@ -993,8 +993,7 @@ get_python_function(string module, string class, int instance_id, string func) {
993993

994994
// Set the Python search path to be the current working directory
995995
char cwd[PATH_MAX];
996-
if ( getcwd(cwd, sizeof(cwd)) == NULL)
997-
{
996+
if ( getcwd(cwd, sizeof(cwd)) == NULL) {
998997
error_print_and_exit("Failed to get the current working directory.");
999998
}
1000999

@@ -1004,7 +1003,7 @@ get_python_function(string module, string class, int instance_id, string func) {
10041003

10051004
Py_SetPath(wcwd);
10061005

1007-
DEBUG_PRINT("Loading module %s in %s.", module, cwd);
1006+
DEBUG_PRINT("Loading module %s in %s.", module, cwd);
10081007

10091008
pModule = PyImport_Import(pFileName);
10101009

@@ -1020,6 +1019,8 @@ get_python_function(string module, string class, int instance_id, string func) {
10201019
if (pDict == NULL) {
10211020
PyErr_Print();
10221021
error_print("Failed to load contents of module %s.", module);
1022+
/* Release the thread. No Python API allowed beyond this point. */
1023+
PyGILState_Release(gstate);
10231024
return 1;
10241025
}
10251026

@@ -1042,6 +1043,8 @@ get_python_function(string module, string class, int instance_id, string func) {
10421043
if (pClasses == NULL){
10431044
PyErr_Print();
10441045
error_print("Failed to load class list \"%s\" in module %s.", class, module);
1046+
/* Release the thread. No Python API allowed beyond this point. */
1047+
PyGILState_Release(gstate);
10451048
return 1;
10461049
}
10471050

@@ -1051,6 +1054,8 @@ get_python_function(string module, string class, int instance_id, string func) {
10511054
if (pClass == NULL) {
10521055
PyErr_Print();
10531056
error_print("Failed to load class \"%s[%d]\" in module %s.", class, instance_id, module);
1057+
/* Release the thread. No Python API allowed beyond this point. */
1058+
PyGILState_Release(gstate);
10541059
return 1;
10551060
}
10561061

@@ -1066,6 +1071,8 @@ get_python_function(string module, string class, int instance_id, string func) {
10661071
if (pFunc && PyCallable_Check(pFunc)) {
10671072
DEBUG_PRINT("Calling function %s from class %s[%d].", func , class, instance_id);
10681073
Py_INCREF(pFunc);
1074+
/* Release the thread. No Python API allowed beyond this point. */
1075+
PyGILState_Release(gstate);
10691076
return pFunc;
10701077
}
10711078
else {
@@ -1086,11 +1093,8 @@ get_python_function(string module, string class, int instance_id, string func) {
10861093

10871094
DEBUG_PRINT("Done with start().");
10881095

1089-
if (is_initialized == 0) {
1090-
/* We are the first to initilize the Pyton interpreter. Destroy it when done. */
1091-
Py_FinalizeEx();
1092-
}
1093-
10941096
Py_INCREF(Py_None);
1097+
/* Release the thread. No Python API allowed beyond this point. */
1098+
PyGILState_Release(gstate);
10951099
return Py_None;
10961100
}

0 commit comments

Comments
 (0)