Here is a decorator that you can wrap around a function to add a timeout.
It works on windows as well as linux/macos.
from multiprocessing import Process, Pipe
import sys, os
class TimeoutError(Exception): pass
def spawn(f): # A decorator that calls a function given a pipe and returns the return value through it.
def func(pipe, args, kwargs, stdout, stdin):
sys.stdout = stdout
sys.stdin = stdin
pipe.send(f(*args, **kwargs)) #Pass the return arguments through the pipe
pipe.close() # Close the pipe
return func
def timeout(time):
def decorator(func):
def run_timeout(*args, **kwargs):
pipe_1, pipe_2 = Pipe()
stdin = os.fdopen(os.dup(sys.stdin.fileno()))
p = Process(target=spawn(func), args=(pipe_2, args, kwargs, sys.stdout, stdin))
p.start() # Start the function
p.join(time) # Join it back to the main process, max time seconds
if p.is_alive(): # If it timed out
p.terminate() # Kill the process
raise TimeoutError("Function %s timed out. (Took %s seconds)"%(repr(func), time)) # raise the error
return pipe_1.recv() # return the return value
return run_timeout
return decorator
Use it like this
@timeout(5) # Times out after 5 seconds
def get_input(prompt):
return raw_input(prompt)
If the function doesn't return, it raises a TimeoutError
EDIT: Fixed so that stdin could be used in the second process.