exec code with timeout?

Graham Fawcett fawcett at teksavvy.com
Mon Sep 8 07:35:34 CEST 2003

OKB (not okblacke) wrote:

>Graham Fawcett wrote:
>>The RestrictedPython module in Zope may suit your needs; it was
>>designed for a
>>similar use scenario. Search the list, there was a discussion about
>>RestrictedPython not long ago.
>    	Thanks.  I looked at the page that was referenced from here, but it 
>says RestrictedPython "doesn't implement resource limitations, like 
>preventing scripts from eating up all available RAM or simply never 
>terminating".  Preventing scripts from never terminating is exactly what 
>I am interested in.  (This isn't a halting problem kind of thing -- I 
>just want to unceremoniously kill exec-ed code that doesn't terminate in 
>a given amount of time.)  Also, it seems a bit heavyweight.  The page 
>implies that it uses a modified compiler, etc.; I don't know how that 
>would work with running interactively edited code.

Hmm... well, again I'd suggest the PyThreadState_SetAsyncExc function. It
shouldn't be that hard to wrap... I'd try it myself, but haven't made it 
to 2.3 yet.  :(

Or, if you're in Unix-land, how about forking? Let the user code
run in the child fork, and let the other half take care of the timeout?
It's easy to kill processes.

-- G

More information about the Python-list mailing list