from __future__ import print_function import signal import threading def handler(signum, frame): print('hi') def a(): while True: pass threading.Thread(target=a).start() signal.signal(signal.SIGALRM, handler) signal.alarm(2) #set this to True and the signal will trigger. if False: a()