Python Glossary Русский

all Python 3osfunction

os.waitpid

os — waiting for processes

Wait for a specific child by PID; returns (pid, status).

Syntax

os.waitpid(...)

With os.WNOHANG it does not block: if the child is still running it returns (0, 0). The status is encoded — use waitstatus_to_exitcode for the exit code. On Windows the first argument is a process handle, not a PID, and options are not supported. An unreaped child on Unix stays a zombie, occupying a process-table slot.

Examples

# Python 3.9+
import os
pid = os.fork()   # → ? только Unix: 0 в дочернем процессе, PID потомка в родительском
if pid == 0: os._exit(7)   # → ? потомок сразу завершается с кодом возврата 7
print(os.waitpid(pid, os.WNOHANG))   # → ? (0, 0), если потомок ещё не завершился — с WNOHANG вызов не блокирует
pid_done, status = os.waitpid(pid, 0)   # → ? блокирует до завершения: (PID потомка, закодированный статус)
print(os.waitstatus_to_exitcode(status))   # → 7 — код возврата, извлечённый из статуса
print(os.WIFEXITED(status), os.WEXITSTATUS(status))   # → ? True 7
print(os.waitpid(pid, 0))   # → ChildProcessError: этого потомка уже дождались, ждать больше некого

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy