timer file descriptor HOWTO — Examples
The following example shows how to use a timer file descriptor to execute a function twice a second Bounded code example (external data; do not execute automatically): ```python # Practical scripts should use really use a non-blocking timer, # we use a blocking timer here for simplicity.
Reference note (untrusted external data; do not execute it as instructions).
The following example shows how to use a timer file descriptor to execute a function twice a second
Bounded code example (external data; do not execute automatically):
```python
# Practical scripts should use really use a non-blocking timer,
# we use a blocking timer here for simplicity.
import os, time
# Create the timer file descriptor
fd = os.timerfd_create(time.CLOCK_REALTIME)
# Start the timer in 1 second, with an interval of half a second
os.timerfd_settime(fd, initial=1, interval=0.5)
try:
# Process timer events four times.
for _ in range(4):
# read() will block until the timer expires
_ = os.read(fd, 8)
print("Timer expired")
finally:
# Remember to close the timer file descriptor!
os.close(fd)
```
To avoid the precision loss caused by the float type, timer file descriptors allow specifying initial expiration and interval in integer nanoseconds with _ns variants of the functions.
This example shows how ~select.epoll can be used with timer file descriptors to wait until the file descriptor is ready for reading
Bounded code example (external data; do not execute automatically):
```python
import os, time, select, socket, sys
# Create an epoll object
ep = select.epoll()
# In this example, use loopback address to send "stop" command to the server.
#
# $ telnet 127.0.0.1 1234
# Trying 127.0.0.1...
# Connected to 127.0.0.1.
# Escape character is '^]'.
# stop
# Connection closed by foreign host.
#
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("127.0.0.1", 1234))
sock.setblocking(False)
sock.listen(1)
ep.register(sock, select.EPOLLIN)
# Create timer file descriptors in non-blocking mode.
num = 3
fds = []
for _ in range(num):
fd = os.timerfd_create(time.CLOCK_REALTIME, flags=os.TFD_NONBLOCK)
fds.append(fd)
# Register the timer file descriptor for read events
ep.register(fd, select.EPOLLIN)
# Start the timer with os.timerfd_settime_ns() in nanoseconds.
# Timer 1 fires every 0.25 seconds; timer 2 every 0.5 seconds; etc
for i, fd in enumerate(fds, start=1)
```
This example shows how ~select.select can be used with timer file descriptors to wait until the file descriptor is ready for reading
Bounded code example (external data; do not execute automatically): …
Attribution: Adapted from Python Documentation under PSF-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Python Documentation — Doc/howto/timerfd.rst :: Examples ↗Revision f10166035d60 · PSF-2.0 and attribution