@@ -254,7 +254,15 @@ static PyObject* py_request_stop(PyObject *self) {
254
254
static PyObject * py_main (PyObject * self , PyObject * args ) {
255
255
DEBUG_PRINT ("Initializing main." );
256
256
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
257
264
lf_reactor_c_main (1 , argv );
265
+ Py_END_ALLOW_THREADS
258
266
259
267
Py_INCREF (Py_None );
260
268
return Py_None ;
@@ -966,25 +974,17 @@ PyObject* convert_C_action_to_py(void* action) {
966
974
*/
967
975
PyObject *
968
976
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
-
977
977
DEBUG_PRINT ("Starting the function start()." );
978
978
979
979
// Necessary PyObject variables to load the react() function from test.py
980
980
PyObject * pFileName , * pModule , * pDict , * pClasses , * pClass , * pFunc ;
981
981
982
982
PyObject * rValue ;
983
983
984
- // Initialize the Python interpreter
985
- Py_Initialize ();
986
984
987
- DEBUG_PRINT ("Initialized the Python interpreter." );
985
+
986
+ PyGILState_STATE gstate ;
987
+ gstate = PyGILState_Ensure ();
988
988
989
989
// If the Python module is already loaded, skip this.
990
990
if (globalPythonModule == NULL ) {
@@ -993,8 +993,7 @@ get_python_function(string module, string class, int instance_id, string func) {
993
993
994
994
// Set the Python search path to be the current working directory
995
995
char cwd [PATH_MAX ];
996
- if ( getcwd (cwd , sizeof (cwd )) == NULL )
997
- {
996
+ if ( getcwd (cwd , sizeof (cwd )) == NULL ) {
998
997
error_print_and_exit ("Failed to get the current working directory." );
999
998
}
1000
999
@@ -1004,7 +1003,7 @@ get_python_function(string module, string class, int instance_id, string func) {
1004
1003
1005
1004
Py_SetPath (wcwd );
1006
1005
1007
- DEBUG_PRINT ("Loading module %s in %s." , module , cwd );
1006
+ DEBUG_PRINT ("Loading module %s in %s." , module , cwd );
1008
1007
1009
1008
pModule = PyImport_Import (pFileName );
1010
1009
@@ -1020,6 +1019,8 @@ get_python_function(string module, string class, int instance_id, string func) {
1020
1019
if (pDict == NULL ) {
1021
1020
PyErr_Print ();
1022
1021
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 );
1023
1024
return 1 ;
1024
1025
}
1025
1026
@@ -1042,6 +1043,8 @@ get_python_function(string module, string class, int instance_id, string func) {
1042
1043
if (pClasses == NULL ){
1043
1044
PyErr_Print ();
1044
1045
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 );
1045
1048
return 1 ;
1046
1049
}
1047
1050
@@ -1051,6 +1054,8 @@ get_python_function(string module, string class, int instance_id, string func) {
1051
1054
if (pClass == NULL ) {
1052
1055
PyErr_Print ();
1053
1056
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 );
1054
1059
return 1 ;
1055
1060
}
1056
1061
@@ -1066,6 +1071,8 @@ get_python_function(string module, string class, int instance_id, string func) {
1066
1071
if (pFunc && PyCallable_Check (pFunc )) {
1067
1072
DEBUG_PRINT ("Calling function %s from class %s[%d]." , func , class , instance_id );
1068
1073
Py_INCREF (pFunc );
1074
+ /* Release the thread. No Python API allowed beyond this point. */
1075
+ PyGILState_Release (gstate );
1069
1076
return pFunc ;
1070
1077
}
1071
1078
else {
@@ -1086,11 +1093,8 @@ get_python_function(string module, string class, int instance_id, string func) {
1086
1093
1087
1094
DEBUG_PRINT ("Done with start()." );
1088
1095
1089
- if (is_initialized == 0 ) {
1090
- /* We are the first to initilize the Pyton interpreter. Destroy it when done. */
1091
- Py_FinalizeEx ();
1092
- }
1093
-
1094
1096
Py_INCREF (Py_None );
1097
+ /* Release the thread. No Python API allowed beyond this point. */
1098
+ PyGILState_Release (gstate );
1095
1099
return Py_None ;
1096
1100
}
0 commit comments