right way to run some code with timeout in Python right way to run some code with timeout in Python python python

right way to run some code with timeout in Python


What you might be looking for is the multiprocessing module. If subprocess is too heavy, then this may not suit your needs either.

import timeimport multiprocessingdef do_this_other_thing_that_may_take_too_long(duration):    time.sleep(duration)    return 'done after sleeping {0} seconds.'.format(duration)pool = multiprocessing.Pool(1)print 'starting....'res = pool.apply_async(do_this_other_thing_that_may_take_too_long, [8])for timeout in range(1, 10):    try:        print '{0}: {1}'.format(duration, res.get(timeout))    except multiprocessing.TimeoutError:        print '{0}: timed out'.format(duration) print 'end'


A completely general solution to this really, honestly does not exist. You have to use the right solution for a given domain.

  • If you want timeouts for code you fully control, you have to write it to cooperate. Such code has to be able to break up into little chunks in some way, as in an event-driven system. You can also do this by threading if you can ensure nothing will hold a lock too long, but handling locks right is actually pretty hard.

  • If you want timeouts because you're afraid code is out of control (for example, if you're afraid the user will ask your calculator to compute 9**(9**9)), you need to run it in another process. This is the only easy way to sufficiently isolate it. Running it in your event system or even a different thread will not be enough. It is also possible to break things up into little chunks similar to the other solution, but requires very careful handling and usually isn't worth it; in any event, that doesn't allow you to do the same exact thing as just running the Python code.


If it's network related you could try:

import socketsocket.setdefaulttimeout(number)