with timeout(...):

James Stroud jstroud at mbi.ucla.edu
Mon Mar 26 06:59:24 EDT 2007


Nick Craig-Wood wrote:
> Did anyone write a contextmanager implementing a timeout for
> python2.5?
> 
> I'd love to be able to write something like
> 
>     with timeout(5.0) as exceeded:
>         some_long_running_stuff()
>     if exceeded:
>         print "Oops - took too long!"
> 
> And have it work reliably and in a cross platform way!
> 
> From my experiments with timeouts I suspect it won't be possible to
> implement it perfectly in python 2.5 - maybe we could add some extra
> core infrastructure to Python 3k to make it possible?
> 

I'm guessing your question is far over my head, but if I understand it, 
I'll take a stab:

First, did you want the timeout to kill the long running stuff?

I'm not sure if its exactly what you are looking for, but I wrote a 
timer class that does something like you describe:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/464959

Probably you can do whatever you want upon timeout by passing the 
appropriate function as the "expire" argument.

This works like a screen saver, etc.

James



More information about the Python-list mailing list