Python timeout context manager with threads
问题 I have timeout context manager that works perfectly with signals but it raises error in multithread mode because signals work only in main thread. def timeout_handler(signum, frame): raise TimeoutException() @contextmanager def timeout(seconds): old_handler = signal.signal(signal.SIGALRM, timeout_handler) signal.alarm(seconds) try: yield finally: signal.alarm(0) signal.signal(signal.SIGALRM, old_handler) I've seen decorator implementation of timeout but I don't know how to pass yield inside