Python Glossary Русский

Python 3.3+timefunction

time.monotonic()

clocks and timing

A monotonic clock in seconds: it never goes backwards and ignores changes to the system time. Use it for timeouts and deadlines.

Syntax

time.monotonic() -> float

The value itself means nothing — the reference point is undefined (often system boot), only the difference between two calls is meaningful. So you cannot save monotonic() to a file and compare it after a restart, and it does not tell the time of day. For a deadline it is the right choice: if the user changes the clock, a wait loop on time.time() breaks, while one on monotonic() does not.

Examples

import time
deadline = time.monotonic() + 0.05
steps = 0
while time.monotonic() < deadline:
    steps += 1
print(steps > 0)  # → True
print(time.monotonic())  # → ? 12345.678 — точка отсчёта не определена

See also

Open in the full glossarydocs.python.orgReport an inaccuracy