Python Glossary Русский

all Python 3osfunction

os._exit

os — abnormal termination

Exit the process immediately with status n, skipping Python cleanup.

Syntax

os._exit(n)

It skips finally and atexit and does not flush buffers: text printed to a pipe or file may vanish without a trace. Its proper place is a child after os.fork, so the child does not run the parent's cleanup code. Ordinary programs exit via sys.exit, which raises SystemExit and runs all cleanup.

Examples

import os
print(os.getpid())   # → ? идентификатор текущего процесса, например 12345
pid = os.fork()   # → ? только Unix: 0 в дочернем процессе, pid ребёнка в родительском
if pid == 0: os._exit(3)   # → ? дочерний процесс завершается сразу с кодом 3, минуя atexit, finally и сброс буферов
print(os.waitstatus_to_exitcode(os.waitpid(pid, 0)[1]))   # → ? только Unix: 3 — родитель забирает код выхода ребёнка

See also

Open in the full glossarydocs.python.orgReport an inaccuracy