Python Glossary Русский

Python 3.5+osfunction

os.set_blocking

os — descriptor mode

Switch a descriptor between blocking and non-blocking mode. On Windows since Python 3.12, and for pipes only.

Syntax

os.set_blocking(...)

Once non-blocking, os.read and os.write no longer wait but raise BlockingIOError; the code must expect that and partial writes. The flag belongs to the open file, not the number: dup copies and descriptors inherited by children change together. On Windows, since 3.12, it works for pipes only.

Examples

import os
r, w = os.pipe()
os.set_blocking(r, False)   # → только Unix: канал переведён в неблокирующий режим, возвращает None
print(os.get_blocking(r))   # → только Unix: False — режим действительно сменился
print(os.read(r, 10))   # → только Unix: BlockingIOError — данных нет, но чтение не ждёт, а сразу падает
os.set_blocking(r, True)   # → только Unix: вернули блокирующий режим, теперь os.read(r, 10) будет ждать данные

See also

Open in the full glossarydocs.python.orgReport an inaccuracy