f



Embedded Python - Blocking Python Function

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

0
andy1398 (8)
11/14/2007 11:02:42 PM
comp.lang.python 77058 articles. 6 followers. Post Follow

5 Replies
550 Views

Similar Articles

[PageSpeed] 23

En Wed, 14 Nov 2007 20:02:42 -0300, <andy@britishideas.com> escribi�:

> 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.

-- 
Gabriel Genellina

0
gagsl-py2 (3707)
11/14/2007 11:20:01 PM
On Nov 14, 4:20 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
> 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
0
andy1398 (8)
11/15/2007 4:43:58 PM
On Nov 15, 9:43 am, a...@britishideas.com wrote:
> On Nov 14, 4:20 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
> wrote:
>
> > 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

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
0
andy1398 (8)
11/15/2007 7:18:45 PM
En Thu, 15 Nov 2007 16:18:45 -0300, <andy@britishideas.com> escribi�:

> On Nov 15, 9:43 am, a...@britishideas.com wrote:
>> On Nov 14, 4:20 pm, "Gabriel Genellina" <gagsl-...@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

0
gagsl-py2 (3707)
11/16/2007 12:03:07 AM
On Nov 15, 5:03 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
> En Thu, 15 Nov 2007 16:18:45 -0300, <a...@britishideas.com> escribi=F3:
>
> > On Nov 15, 9:43 am, a...@britishideas.com wrote:
> >> On Nov 14, 4:20 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
> >> wrote:
>
> >> > Not forcibly - you need some cooperation from the Main function. Mayb=
e
> >> > 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
0
andy1398 (8)
11/16/2007 10:54:51 PM
Reply: