Timeout test hangs IDLE

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Dec 6 22:33:52 EST 2007


En Thu, 06 Dec 2007 08:03:49 -0300, Andreas Tawn  
<andreas.tawn at ubisoft.com> escribió:

>> 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.
>
> 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?

It's not "your" problem, the cookbook recipe your code is based on should  
make it clear: it does *not* kill the thread, it just returns to the  
caller earlier (at most when the timeout elapses). The thread continues to  
run (and consuming resources, of course).
If you actually want to stop a background task, the safest way would be  
for the thread to test some condition periodically and gracefully stop  
when requested. If that's not possible, and you have to forcibly kill a  
running thread, there is a different recipe at  
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496960 (follow the  
link at the end for an updated version).

> 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?

Not exactly; doing background tasks with a GUI is tricky and requires some  
kind of cooperation, else the interfase usually becomes unresponsive.
How to run background tasks depends on the GUI toolkit used. IDLE uses  
Tkinter, do you use that toolkit for your final program?

-- 
Gabriel Genellina




More information about the Python-list mailing list