Any idea of stopping the execution of PyRun_File()

BigHand heweiwei at gmail.com
Wed Jun 10 14:59:26 CEST 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

hello.Gabriel.
PyThreadState_SetAsyncExc cause me a win32 exception of "Stack
overflow"
>	msvcr80d.dll!_getptd_noexit()  Line 592	C
 	msvcr80d.dll!_getptd()  Line 658 + 0x5 bytes	C
 	msvcr80d.dll!_LocaleUpdate::_LocaleUpdate(localeinfo_struct *
plocinfo=0x00000000)  Line 264 + 0x5 bytes	C++
 	msvcr80d.dll!_output_l(_iobuf * stream=0x10311d40, const char *
format=0x1e26e0ac, localeinfo_struct * plocinfo=0x00000000, char *
argptr=0x000333c4)  Line 1028	C++
 	msvcr80d.dll!fprintf(_iobuf * str=0x10311d40, const char *
format=0x1e26e0ac, ...)  Line 70 + 0x13 bytes	C
 	python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4)  Line 2000
+ 0x19 bytes	C
 	python30_d.dll!PyThreadState_Get()  Line 349 + 0xa bytes	C
 	python30_d.dll!PyErr_Occurred()  Line 133 + 0x5 bytes	C
 	python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4)  Line 2001
+ 0x5 bytes	C
 	python30_d.dll!PyThreadState_Get()  Line 349 + 0xa bytes	C
 	python30_d.dll!PyErr_Occurred()  Line 133 + 0x5 bytes	C
 	python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4)  Line 2001
+ 0x5 bytes	C
 	python30_d.dll!PyThreadState_Get()  Line 349 + 0xa bytes	C
 	python30_d.dll!PyErr_Occurred()  Line 133 + 0x5 bytes	C
 	python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4)  Line 2001
+ 0x5 bytes	C
 	python30_d.dll!PyThreadState_Get()  Line 349 + 0xa bytes	C
 	python30_d.dll!PyErr_Occurred()  Line 133 + 0x5 bytes	C
 	python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4)  Line 2001
+ 0x5 bytes	C
 	python30_d.dll!PyThreadState_Get()  Line 349 + 0xa bytes	C
 	python30_d.dll!PyErr_Occurred()  Line 133 + 0x5 bytes	C
 	python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4)  Line 2001
+ 0x5 bytes	C
 	python30_d.dll!PyThreadState_Get()  Line 349 + 0xa bytes	C
 	python30_d.dll!PyErr_Occurred()  Line 133 + 0x5 bytes	C
 	python30_d.dll!Py_FatalError(const char * msg=0x1e26a2d4)  Line 2001
+ 0x5 bytes	C
 	python30_d.dll!PyThreadState_Get()  Line 349 + 0xa bytes	C

My app is a MFC app, it have the main thread and the sub thread, the
sub thread run the script(Py_RunFile),
That I am looking for a way to stop the Py_RunFile from the main
thread.
that looks like I have to use the TerminateThread() .

thanks.Gabriel, could you find some example for me?



More information about the Python-list mailing list