Any idea of stopping the execution of PyRun_File()

kongcheng1400 at gmail.com kongcheng1400 at gmail.com
Tue Jun 9 21:44:07 EDT 2009


On Jun 10, 7:41 am, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Mon, 08 Jun 2009 22:15:22 -0300, BigHand <hewei... at gmail.com> escribió:
>
> >    I have an embedded python application. which is  a MFC app with
> > Python interpreter embedded.
>
> >    In the App, I have a separate thread to execute a Python script
> > (using the PyRun_File), but if the user want to stop the executing
> > script, how should I do?
>
> > A possible way is terminate the thread of executing the scripts by
> > TerminateThread(). but TerminateThread() is unsafe and not
> > recommended.
>
> > guys, could you guide me on this?
>
> You could use PyThreadState_SetAsyncExc (to "inject" an exception), or  
> perhaps PyErr_SetInterrupt (to emulate ^C, which in turn generates a  
> KeyboardInterrupt). This should work fine for well-behaved scripts, but if  
> it ignores all exceptions like this:
>         try: ...
>         except: pass
> you'll have to look at ceval.c how to break out of the running loop.
>
> (this is yet another argument against indiscriminately using a bare except  
> clause...)
>
> --
> Gabriel Genellina

Thanks Very much, You are always kindly helping me.



More information about the Python-list mailing list