Python 3.3+Linuxosfunction
os.pipe2
os — special descriptors
Like pipe, but atomically sets flags (e.g. O_NONBLOCK/O_CLOEXEC). Availability: Linux.
Syntax
os.pipe2(...)
It sets flags atomically at creation, without separate calls per end: os.pipe2(os.O_NONBLOCK | os.O_CLOEXEC). Atomicity matters in a multithreaded program, where another thread could launch a child between creating the pipe and setting its flags.
Examples
import os
r, w = os.pipe2(os.O_NONBLOCK | os.O_CLOEXEC) # → ? только Linux: пара дескрипторов, сразу неблокирующих и закрываемых при exec
print(os.write(w, b'ping')) # → 4
print(os.read(r, 4)) # → b'ping'
os.read(r, 4) # → BlockingIOError — данных больше нет, но из-за O_NONBLOCK вызов не зависает
r2, w2 = os.pipe() # переносимый вариант без флагов: O_NONBLOCK/O_CLOEXEC пришлось бы доставлять отдельными вызовами fcntl, оставляя окно гонки при fork
See also
Open in the full glossarydocs.python.orgReport an inaccuracy