with timeout(...):

Diez B. Roggisch deets at nospam.web.de
Mon Mar 26 14:16:27 CEST 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!

Cross platform isn't the issue here - reliability though is. To put it
simple: can't be done that way. You could of course add a timer to the
python bytecode core, that would "jump back" to a stored savepoint or
something like that.

But to make that work reliably, it has to be ensured that no sideeffects
occur while being in some_long_running_stuff. which doesn't only extend to
python itself, but also external modules and systems (file writing, network
communications...). Which can't be done, unless you use a time-machine.
Which I'd take as an personal insult, because in that rolled-back timeframe
I will be possibly proposing to my future wife or something...

Diez



More information about the Python-list mailing list