Python Glossary Русский

Python 3.3+faulthandlerfunction

faulthandler.dump_traceback()

dumping the stack

Immediately writes the call stack of all threads (or just the current one) to a file — without an exception and without stopping the program.

Syntax

faulthandler.dump_traceback(file=sys.stderr, all_threads=True)

It writes straight to the file descriptor, bypassing Python's buffer: when dumping to sys.stdout, earlier print() output may appear after the dump, so call sys.stdout.flush() first. The format is poorer than traceback's — only file, line and function name, no source text — but it is safe where the interpreter is already in bad shape. With all_threads=True the first line carries a thread id.

Examples

import faulthandler
def work():
    with open('stack.txt', 'w') as f:
        faulthandler.dump_traceback(file=f, all_threads=False)
work()
text = open('stack.txt').read()
print(text.splitlines()[0])  # → Stack (most recent call first):
print('in work' in text)  # → True
import faulthandler, sys
print('до дампа')
sys.stdout.flush()
faulthandler.dump_traceback(file=sys.stdout, all_threads=False)  # → ? путь к файлу примера

See also

Open in the full glossarydocs.python.orgReport an inaccuracy