Embedded Python - Blocking Python Function

A

andy

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
 
G

Gabriel Genellina

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?

Not forcibly - you need some cooperation from the Main function. Maybe
setting a global variable that Main checks periodically.
 
A

andy

Not forcibly - you need some cooperation from the Main function. Maybe
setting a global variable that Main checks periodically.

Thanks. I'll give that a try!

Andy
 
A

andy

Thanks. I'll give that a try!

Andy

It works but the problem is that the script will be written by the end
user. If they make a mistake and the cancel flag isn't perodically
checked then it seems I have no way of cleanly ending the interpreter.
If I wait for a specific time after requesting the Main function stop
I need to be able to kill the interpreter without a runtime error. Any
ideas?

Andy
 
G

Gabriel Genellina

It works but the problem is that the script will be written by the end
user. If they make a mistake and the cancel flag isn't perodically
checked then it seems I have no way of cleanly ending the interpreter.
If I wait for a specific time after requesting the Main function stop
I need to be able to kill the interpreter without a runtime error. Any
ideas?

You could use PyThreadState_SetAsyncExc - it's supposed to raise an
exception in another thread. There is a Cookbook recipe using it here
<http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496960>
I've never actually used it, but I want to try it some day, so please
report back your findings if you decide to use this function.
 
A

andy

You could use PyThreadState_SetAsyncExc - it's supposed to raise an
exception in another thread. There is a Cookbook recipe using it here
<http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496960>
I've never actually used it, but I want to try it some day, so please
report back your findings if you decide to use this function.


It seems this is the function I need, however the following gave an
access violation:

PyEval_AcquireLock();
PyThreadState_Swap(thread);

// stop interpreter by sending system exit exception to it's thread
PyThreadState_SetAsyncExc(thread->thread_id, PyExc_SystemExit);

PyThreadState_Swap(maininterpreter);
PyEval_ReleaseLock();

Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top