Embedded Python - Blocking Python Function

andy at britishideas.com andy at britishideas.com
Wed Nov 14 18:02:42 EST 2007


I embed multiple interpreters. I create the interpreter and modules in
the primary thread of my application:

PyEval_AcquireLock();
thread = Py_NewInterpreter();
PyThreadState_Swap(thread);

...initialize modules, etc....

PyThreadState_Swap(maininterpreter);
PyEval_ReleaseLock();

Then I create a C thread called "main" which calls a function called
"Main" in the Python interpreter:

PyEval_AcquireLock();
PyThreadState_Swap(thread);
moduledictionary = PyModule_GetDict(pmodule);
PyObject_CallObject(PyDict_GetItemString(moduledictionary, "Main"),
NULL);
PyThreadState_Swap(maininterpreter);
PyEval_ReleaseLock();

The problem is that the function "Main" in the Python script can take
up to 60 seconds to execute. How can I terminate this thread (and
therefore the Main function in python) cleanly from the primary thread
of my application?

If I try to call Py_EndInterpreter(thread); then I get a runtime error
(presumably because the Main function is still executing).

thanks, Andy




More information about the Python-list mailing list