embedded python and endless loops

Warren Postma warrenpstma at ____hotmail__.com
Sat Mar 8 01:19:23 CET 2003

On Thu, 06 Mar 2003 13:35:07 -0800, Mike Weiss wrote:

> My C++ program (a Windows-based server application) uses embedded
> Python (2.2) to execute user-defined scripts. Recently end-users have
> coded (by mistake) endless loops which, when run, hang my app.
> Is there a way to halt the execution of the very high-level functions
> like PyRun_String()? I'm thinking I could have a second thread (in my
> c++ code) wait for a certain period of time (a timeout) and then
> somehow stop the Python interepter? But how to stop it? (something
> with a keyboard interupt?)
> The solution has to work on Windows.

I have implemented something like this. I had to hack the Python
interpreter code to do it, but that was in Python 1.5.2.

In a later version (2.2 or later) you can use a C Profiling API to hook
into the code, and you can set a global variable to terminate the running
of the code.  How you might decide to allow the user to terminate their
code, or if you merely force a termination after some time period, is up
to you.  I recommend against a time period (even several minutes) as it
will limit the utility of your scripting engine.

Having the ability to cancel execution of a script that never terminates,
on its own is a nice feature.  Don't think you can detect automatically
when a script has gone goo-goo, though, because you might one day want a
long running script, that takes hours to run, or longer. <grin>


More information about the Python-list mailing list