Embedded Python - Blocking Python Function

andy at britishideas.com andy at britishideas.com
Fri Nov 16 23:54:51 CET 2007


On Nov 15, 5:03 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Thu, 15 Nov 2007 16:18:45 -0300, <a... at britishideas.com> escribió:
>
> > On Nov 15, 9:43 am, a... at britishideas.com wrote:
> >> On Nov 14, 4:20 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
> >> wrote:
>
> >> > Not forcibly - you need some cooperation from the Main function. Maybe
> >> > setting a global variable that Main checks periodically.
>
> > 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.
>
> --
> Gabriel Genellina


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



More information about the Python-list mailing list