class Timeout:
    """Timeout class using ALARM signal"""


class Timeout(Exception):
    pass

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

def __enter__(self):
    signal.signal(signal.SIGALRM, self.raise_timeout)
Try - Except in Python for given amount of time