import time, signal hit = 0 def noop(*args): global hit hit += 1 signal.signal(signal.SIGINT, noop) if hasattr(signal, 'SIGALRM'): signal.signal(signal.SIGALRM, noop) signal.setitimer(signal.ITIMER_REAL, 0.1, 0.1) print("sleep 3 seconds... press CTRL+c multiple times") t0 = time.monotonic() time.sleep(3) if hasattr(signal, 'SIGALRM'): signal.alarm(0) dt = time.monotonic() - t0 print(dt, "elapsed") print(hit, "signals")