Python Glossary Русский

all Python 3osfunction

os.getppid

os — process

Return the parent process id.

Syntax

os.getppid()

If the parent has exited, Unix returns the PID of init (1) or of a designated subreaper, while Windows returns the old number, which may already belong to another process. So checking whether the parent is alive via getppid is reliable only on Unix, by comparing with the original value.

Examples

import os
print(os.getppid())   # → ? PID родительского процесса, например 4312 (для скрипта из терминала — это PID оболочки)
print(os.getppid() == os.getpid())   # → ? False: getppid() возвращает родителя, а getpid() — сам процесс
if os.fork() == 0: print(os.getppid())   # → ? только Unix: дочерний процесс печатает PID родителя — то же число, что вернул os.getpid() до fork
ppid = os.getppid()   # → ? ловушка: если родитель уже завершился, процесс-сирота «усыновляется» — на Linux обычно 1 (init/systemd)

See also

Open in the full glossarydocs.python.orgReport an inaccuracy