Python Glossary Русский

Python 3.10+Linuxosfunction

os.eventfd

os — events and timers (fd)

Create an event-counter file descriptor for signalling (3.10+). Availability: Linux.

Syntax

os.eventfd(...)

It creates an in-kernel counter: a write adds to it, a read returns the accumulated sum and resets it. The descriptor works with select and selectors — handy for waking an event loop from another thread. With EFD_SEMAPHORE each read decrements the counter by one and returns 1.

Examples

import os
fd = os.eventfd(0)   # → ? только Linux: новый дескриптор-счётчик со стартовым значением 0
os.eventfd_write(fd, 3)   # → ? только Linux: счётчик 0 → 3, fd становится готовым для чтения
print(os.eventfd_read(fd))   # → 3 — чтение забирает значение и обнуляет счётчик
os.close(fd)   # → ? только Linux: eventfd закрывается как обычный файловый дескриптор
print(os.eventfd_read(os.eventfd(2, os.EFD_SEMAPHORE | os.EFD_NONBLOCK)))   # → ? только Linux: 1, в семафорном режиме чтение снимает по одной единице

See also

Open in the full glossarydocs.python.orgReport an inaccuracy