Skip to content

Instantly share code, notes, and snippets.

@fordhurley
Created January 22, 2015 13:56
Show Gist options
  • Save fordhurley/dae1a012da2b3fb72291 to your computer and use it in GitHub Desktop.
Save fordhurley/dae1a012da2b3fb72291 to your computer and use it in GitHub Desktop.
Implementing a timeout in python with multiprocessing
import time
import multiprocessing as mp
from Queue import Empty
def timed_run(func, args=(), kwargs={}, timeout=10, default=None):
def run_func(q):
result = func(*args, **kwargs)
q.put(result)
queue = mp.Queue()
p = mp.Process(target=run_func, args=(queue,))
p.start()
print 'calling %s for %r seconds' % (func, timeout)
started_at = time.time()
p.join(timeout)
ended_at = time.time()
diff = round(ended_at - started_at)
print '%s exited after %r seconds' % (func, diff)
if p.is_alive():
print 'terminating process %s' % p
p.terminate()
try:
return queue.get(False)
except Empty:
return default
def loop_forever():
while True:
time.sleep(1)
print 'still sleeping (forever)'
def loop_for(seconds):
i = 0
while i < seconds:
time.sleep(1)
print 'still sleeping (for %d seconds)' % seconds
i += 1
return i
if __name__ == '__main__':
result = timed_run(loop_forever, timeout=5, default=-1)
print 'Result:', result
print
result = timed_run(loop_for, args=(10,), timeout=5, default=-1)
print 'Result:', result
print
result = timed_run(loop_for, args=(3,), timeout=5, default=-1)
print 'Result:', result
print
result = timed_run(loop_for, args=(20,), timeout=5, default=-1)
print 'Result:', result
@fordhurley
Copy link
Author

Output:

calling <function loop_forever at 0x101f959b0> for 5 seconds
still sleeping (forever)
still sleeping (forever)
still sleeping (forever)
still sleeping (forever)
<function loop_forever at 0x101f959b0> exited after 5.0 seconds
terminating process <Process(Process-1, started)>
Result: -1

calling <function loop_for at 0x101f95a28> for 5 seconds
still sleeping (for 10 seconds)
still sleeping (for 10 seconds)
still sleeping (for 10 seconds)
still sleeping (for 10 seconds)
<function loop_for at 0x101f95a28> exited after 5.0 seconds
terminating process <Process(Process-2, started)>
Result: -1

calling <function loop_for at 0x101f95a28> for 5 seconds
still sleeping (for 3 seconds)
still sleeping (for 3 seconds)
still sleeping (for 3 seconds)
<function loop_for at 0x101f95a28> exited after 3.0 seconds
Result: 3

calling <function loop_for at 0x101f95a28> for 5 seconds
still sleeping (for 20 seconds)
still sleeping (for 20 seconds)
still sleeping (for 20 seconds)
still sleeping (for 20 seconds)
<function loop_for at 0x101f95a28> exited after 5.0 seconds
terminating process <Process(Process-4, started)>
Result: -1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment