Oslo/blueprints/time monotonic
Authors
- Victor Stinner - victor.stinner@enovance.com
Rationale
Using time.time() to compute timeouts is wrong. The elapsed time may be wrong on update of the system clock. A monotonic clock should be used to avoid this issue. I propose to add a timeutils.time_monotonic() function. It will use time.monotonic() if available, or fallback to clock_getclock(CLOCK_MONOTONIC) on Linux.
For a longer rationale, read the PEP 418.
Use time.monotonic()
Eventlet should use time_monotonic() instead of time.time(). Example of bug of time.time(): openstack-dev: time.sleep is affected by eventlet.monkey_patch().
For greenlet, the monotonic clock can be used with something like that:
eventlet.hubs._threadlocal.hub = eventlet.hubs.get_default_hub().Hub(monotonic_time)
time_monotonic() should be used for any function computing a timeout. Recent example: oslo.messaging: Add an optional timeout parameter to Listener.poll
Implementation
The following patch is based on the file time_monotonic.py from my Trollius project. Trollius is tested on Linux, Mac OS X, Windows, FreeBSD and OpenIndiana (which is almost Solaris): timeutils: add time_monotonic() function. The reimplement reuses time.monotonic() if available.
Alternatives
- https://pypi.python.org/pypi/Monotime
- On Linux, it uses CLOCK_MONOTONIC_RAW which is wrong according to my PEP 418. Extract: "Slewing is generally desirable (i.e. we should use CLOCK_MONOTONIC, not CLOCK_MONOTONIC_RAW) if one wishes to measure "real" time (and not a time-like object like CPU cycles)."
- Monotime module doesn't support Windows nor Solaris. Does it matters for Oslo Incubator?
- It doesn't reuse time.monotonic() if available.
- python-monotonic-time
- It doesn't look to be available on PyPI: https://github.com/gavinbeatty/python-monotonic-time