all Python 3Linux · macOSosfunction
os.wait4
os — waiting for processes
Like waitpid, but also returns rusage. Availability: Unix.
Syntax
os.wait4(...)
It waits for a specific child and returns its resource usage — handy to learn the peak memory or CPU time of a launched program without external tools. ru_maxrss is in kilobytes on Linux and bytes on macOS. WNOHANG works as with waitpid.
Examples
import os
if os.fork() == 0: os._exit(7) # → ? только Unix: ребёнок завершается кодом 7, родитель продолжает скрипт
child, status, rusage = os.wait4(-1, 0) # pid=-1 — дождаться любого ребёнка; options=0 — блокирующе
print(child, os.waitstatus_to_exitcode(status)) # → ? только Unix: pid ребёнка и 7, например «12346 7»
print(rusage.ru_utime) # → ? только Unix: пользовательское процессорное время ребёнка в секундах, например 0.002 — os.waitpid такого не даёт
print(os.wait4(-1, 0)) # → ChildProcessError — статус уже забран, второй раз его не получить
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy