
How (not) to set a timeout on a computation in Python - dagw
http://eli.thegreenplace.net/2011/08/22/how-not-to-set-a-timeout-on-a-computation-in-python/
======
z0r
We use this method where I work :) It's true that if the thread hasn't
finished by the end of the time limit, an exception will be raised and the
thread will keep running. Part of our solution is that the thread is also
aware of the time limit. It checks to see if it has timed out at regular
intervals during its calculations, and quickly exits itself if it has expired.
It's not the prettiest thing on earth, but it gets the job done for us

------
Dylan16807
The article dismisses setting an exception in the same line it's mentioned but
it seems to me like a good option. As long as it's a _python_ function then
any incidental c calls will quickly return and let it abort, right? The
difficulty of stopping a c function is a whole different ballgame.

~~~
eliben
The problem is providing a generic solution that will work for all kinds of
functions. Much Python code uses C calls under the hood, so this isn't easy to
do.

------
robfig
Isn't the usual way to do this is to just pass down a "timeout" parameter and
have the method take care of time limiting itself? That always seemed to work
well for me. (And APIs provide such an interface if they have long-running
blocking methods)

------
mkramlich
Shotgun solution: make the target fn be a process. Have a 2nd process be a
watchdog and kill the target process if it exceeds the time limit. Design the
arch of this system such that no matter what the target proc was doing
precisely when killed, that the system can recover/resume reasonably well.

------
DrJ

      In this case, if a timeout is required, it’s recommended to
      use asynchronous IO which naturally supports interruptions.
      Unfortunately, asynchronous IO also makes code more 
      convoluted and difficult to write.
    

if you are bad at Async Coding ;)

------
yaroslavvb
Another problem with SIGALARM is that there's only one of them, so setting
second alarm will reset the previous counter, and make the system try to run
both handlers at the same time on trigger

