class timeout(object):

    def __init__(self, seconds):
        self.seconds = seconds

def __enter__(self):
    self.die_after = time.time() + self.seconds
return self

def __exit__(self, type, value, traceback):
    pass

@property
def timed_out(self):
    return time.time() > self.die_after
Python timeout context manager with threads