exec code with timeout?

OKB (not okblacke) BrenBarn at aol.com
Mon Sep 8 05:36:38 CEST 2003

    	I am fiddling around with a Python-based MUD which allows users to 
code MUD objects in Python.  This code is executed from within the 
server code with "exec".  However, sometimes errors in user code can 
result in infinite loops, which cause the MUD to hang.  I am wondering 
if there is a way to put a timeout on the exec, so that it returns 
control to the main program if the user code doesn't return within a 
certain amount of time.

    	I am not concerned with security per se here -- in general I can 
assume that any users who are using the MUD are trusted and will not 
write malicious code.  I am chiefly aiming to catch simple, 
unintentional programming errors. For instance, I don't want the MUD 
to hang just because someone innocently forgot to include a break 
inside a "while 1:" block.  All I need is a very crude form of control 
-- a simple timeout would do.

    	I have looked at the thread modules, hoping that I could exec the 
user code in a separate thread, sleep in the main thread, and then at 
the end of the time limit terminate the user code thread.  However, 
there doesn't seem to be any way to terminate a subthread from within 
the main thread.  Am I entirely out of luck here, or is there some 
module than can do what I want?  (I am working primarily on Windows at 
this point, but I would like a solution that works on as many 
platforms as as possible.)

--OKB (not okblacke)
"Do not follow where the path may lead.  Go, instead, where there is
no path, and leave a trail."
	--author unknown

More information about the Python-list mailing list