[Pythonmac-SIG] Hung like a snake...
Steven D. Majewski
sdm7g@virginia.edu
Thu, 10 Sep 1998 16:11:52 -0400 (EDT)
On 10 Sep 1998, Jonathan "Hung like a snake" Wight wrote:
> If I were to create a mac app that could be extended with Python scripts
> how would I gracefully deal with the possibility of a script getting
> into an infinite loop and hanging?
>
> Is threading the answer? Is threading implemented properly on the Mac?
I haven't tried threading & Python on the Mac, however, you may note
that when you run the Mac Python application, [1] you can background
the app by switching to another foreground app, and [2] you can cancel
a long command with Command-\period.
If you look at the main interpreter loop in ceval.c, just before the
#ifdef WITH_THREAD block of code, you'll see:
#if !defined(HAVE_SIGNAL_H) || defined(macintosh)
/* If we have true signals, the signal handler
will call Py_AddPendingCall() so we don't
have to call sigcheck(). On the Mac and
DOS, alas, we have to call it. */
if (PyErr_CheckSignals()) {
why = WHY_EXCEPTION;
goto on_error;
}
#endif
PyErr_CheckSignals() [ in sigcheck.c ] calls PyOS_InterruptOccurred()
The "OS" indicates that this is an OS specific function, and it's
defined in macglue.c -- and that's where the check for Command-period
is done.
The exact flow of control when you add Tkinter and SOUIX and apple
events into the mix is something I haven't quite unravelled, but
SOMEWHERE MacPython makes occasional calls to PyMac_DoYield ( in
macglue.c in the Mac/Python sources. ) to check for SOUIX and apple
events.
If you are embedding the python interpreter in another non-python
app, then I believe you'll probably be calling some other, lower
level entry point to the interpreter (in pythonrun.c), but you
might do some similar setup in your app before calling the
interpreter.
> Is there some other way of detecting an infinite loop (timeouts) and
> then gracefully killing it and _hopefully_ rolling back the changes the
> script has made (if needed).
However, *detecting* an infinite loop is another matter, and is not,
in general, solvable. But you may be able to make some assumptions
within your application to decide when that is likely ( For example,
the python interpreter itself, has a recursion limit to stop
runaway stack growth. ) or allow the user a way to kill the
interpreter.
---| Steven D. Majewski (804-982-0831) <sdm7g@Virginia.EDU> |---
---| Department of Molecular Physiology and Biological Physics |---
---| University of Virginia Health Sciences Center |---
---| P.O. Box 10011 Charlottesville, VA 22906-0011 |---
"I'm not as big a fool as I used to be, I'm a smaller fool." - Jack Kerouac
Some of the Dharma <http://members.aol.com/kerouacsis/SomeDharma.html>