Python try exception with timeout

利用 signal

import signal

def handler(signum, frame):
    raise Exception("timeout")

def push_version():
    import time
    while True:
        print("...")
        time.sleep(1)

signal.signal(signal.SIGALRM, handler)

signal.alarm(10)

try:
    push_version()
except Exception as e:
    if str(e) == "timeout":
        print("timeout")
    else:
        print("eeeeeeeeeee")