Skip to content

Commit 9c59140

Browse files
committed
Add example and mention man page
1 parent f3da4ed commit 9c59140

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

Doc/library/os.rst

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3280,7 +3280,8 @@ features:
32803280

32813281
Create and return an event file descriptor. The file descriptors supports
32823282
raw :func:`read` and :func:`write` with a buffer size of 8,
3283-
:func:`~select.select`, :func:`~select.poll` and similar. By default, the
3283+
:func:`~select.select`, :func:`~select.poll` and similar. See man page
3284+
:manpage:`eventfd(2)` for more information. By default, the
32843285
new file descriptor is :ref:`non-inheritable <fd_inheritance>`.
32853286

32863287
*initval* is the initial value of the event counter. The initial value
@@ -3298,12 +3299,30 @@ features:
32983299
non-zero, :func:`eventfd_read` returns the current event counter value and
32993300
resets the counter to zero.
33003301

3301-
If the event counter is zero, :func:`eventfd_read` blocks.
3302+
If the event counter is zero and :const:`EFD_NONBLOCK` is not
3303+
specified, :func:`eventfd_read` blocks.
33023304

33033305
:func:`eventfd_write` increments the event counter. Write blocks if the
33043306
write operation would increment the counter to a value larger than
33053307
2\ :sup:`64`\ -\ 2.
33063308

3309+
Example::
3310+
3311+
import os
3312+
3313+
# semaphore with start value '1'
3314+
fd = os.eventfd(1, os.EFD_SEMAPHORE | os.EFC_CLOEXEC)
3315+
try:
3316+
# acquire semaphore
3317+
v = os.eventfd_read(fd)
3318+
try:
3319+
do_work()
3320+
finally:
3321+
# release semaphore
3322+
os.eventfd_write(fd, v)
3323+
finally:
3324+
os.close(fd)
3325+
33073326
.. availability:: Linux 2.6.27 or newer with glibc 2.8 or newer.
33083327

33093328
.. versionadded:: 3.10

0 commit comments

Comments
 (0)