Timeout test hangs IDLE

Andreas Tawn andreas.tawn at ubisoft.com
Thu Dec 6 06:03:49 EST 2007


> I once made a small app that used threads on IDLE.
> 
> There was a strange error when using 'print' & threads. When 
> what I printed filled the entire screen, instead of moving 
> all the text up, IDLE just hanged. Try running your code from 
> the shell instead, to see if the problem is in IDLE. 
> 
> HTH,
> Sergio

It looks like there's two issues here, an IDLE problem and a "me"
problem ;o)

I changed the countTest function to time.sleep(10) rather than a big
loop. When I run the script from the command line, the timeout triggers
properly after 5 seconds but the countTest function (running in a
seperate thread now) continues for the whole 10 seconds. This is the
"me" problem. I though that the timeout function would kill the
countTest thread, but it seems like it doesn't do that. Rather, the
timout just returns after 5 seconds with the message that the countTest
thread is still alive. I guess I need to set event flags so the
countTest function can end itself rather than trying to kill it from the
main thread?

When I run it from IDLE, everything runs correctly, but after the
countTest thread ends after 10 seconds, IDLE hangs as before. IDLE bug,
maybe?

Thanks for the help everyone.

The code looks like this now.

def countTest():# Succeeds from the command line, hangs in IDLE
    import time
    time.sleep(10)
    return True

def timeout(func, args=(), kwargs={}, timeout_duration=1, default=None):
    import threading
    class InterruptableThread(threading.Thread):
        def __init__(self):
            threading.Thread.__init__(self)
            self.result = None

        def run(self):
            try:
                self.result = func(*args, **kwargs)
            except:
                self.result = default

    it = InterruptableThread()
    it.start()
    it.join(timeout_duration)
    if it.isAlive():
        return default
    else:
        return it.result

def runTest():
    timeout(countTest, timeout_duration=5)
    print "finished"

if __name__ == "__main__":
    runTest()



More information about the Python-list mailing list