Python 3.3+faulthandlerfunction
faulthandler.dump_traceback_later()
dump on timeout
Starts a watchdog timer: if it is not cancelled within timeout seconds, the stack of all threads is written to a file.
Syntax
faulthandler.dump_traceback_later(timeout, repeat=False, file=sys.stderr, exit=False)
The main tool against hangs: start the timer before a suspicious spot and cancel it after — if the program hangs you see exactly where it is stuck. Calling it again replaces the timer rather than adding a second one. exit=True ends the process via _exit() after the dump, with no buffer flushing and no finally blocks — half-written files stay half-written. repeat=True dumps every timeout seconds.
Examples
import faulthandler, time
with open('hang.txt', 'w') as f:
faulthandler.dump_traceback_later(0.1, file=f)
time.sleep(0.3)
print(open('hang.txt').read().splitlines()[0]) # → Timeout (0:00:00.100000)!
See also
Open in the full glossarydocs.python.orgReport an inaccuracy