Python Glossary Русский

all Python 3Linux · macOSosfunction

os.wait

os — waiting for processes

Wait for any child to finish; returns (pid, status). Availability: Unix.

Syntax

os.wait(...)

It waits for any child, so in a program where different parts spawn children — subprocess included — it may "steal" someone else's status. waitpid with a specific PID is more reliable. With no children it raises ChildProcessError. It returns an encoded status — convert it with waitstatus_to_exitcode.

Examples

import os
pid = os.fork()  # → ? только Unix: 0 в дочернем процессе, pid ребёнка — в родительском
if pid == 0: os._exit(3)  # ребёнок завершается кодом 3 и не выполняет остальной скрипт
child, status = os.wait()  # родитель блокируется здесь, пока не завершится любой из его детей
print(child == pid, os.waitstatus_to_exitcode(status))  # → ? True 3 — дождались того самого ребёнка, из сырого статуса извлечён код выхода
print(os.wait())  # → ChildProcessError — незавершённых детей больше нет, ждать некого

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy