[issue31539] asyncio.sleep may sleep less time then it should

STINNER Victor report at bugs.python.org
Thu Sep 21 12:07:58 EDT 2017


STINNER Victor added the comment:

Clocks is a very complex topic, especially when you care of portability. See my PEP 418.

asyncio uses time.monotonic() clock to not be impacted when the system clock ("wall clock") is updated by the administrator or automatically by NTP.

On Windows, time.monotonic() is implemented with GetTickCount64() which has a bad resolution: around 15 ms. The exact resolution depends on the hardware.

See my PEP 418 which added time.monotonic() for Python 3.3 for much more information:
https://www.python.org/dev/peps/pep-0418/#monotonic-clocks

To get the clock resolution, use:

>>> import time; print(time.get_clock_info('monotonic'))

Example on Linux:

>>> import time; print(time.get_clock_info('monotonic'))
namespace(adjustable=False, implementation='clock_gettime(CLOCK_MONOTONIC)', monotonic=True, resolution=1e-09)

Well, the announced resolution is 1 nanosecond, but in practice it's more around 500 ns. At least, it's better than 15 ms :-)

--

asyncio rounds timings using the clock resolution, time.get_clock_info('monotonic').resolution. Search for self._clock_resolution in Lib/asyncio/base_events.py.

asyncio is not designed for real time.

----------
nosy: +haypo

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue31539>
_______________________________________


More information about the Python-bugs-list mailing list