Jump to: navigation, search

Difference between revisions of "Oslo/blueprints/time monotonic"

(Rationale)
Line 5: Line 5:
 
== Rationale ==
 
== 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.
+
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 [http://legacy.python.org/dev/peps/pep-0418/|PEP 418].
 
For a longer rationale, read the [http://legacy.python.org/dev/peps/pep-0418/|PEP 418].

Revision as of 14:52, 7 April 2014

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 418.

Use time.monotonic()

Eventlet should use time_monotonic() instead of time.time(). Example of bug of time.time(): 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: 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): 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