This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: asyncio scheduler jitter
Type: Stage: resolved
Components: asyncio Versions: Python 3.8
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: QuadCorei8085, asvetlov, yselivanov
Priority: normal Keywords:

Created on 2021-09-14 10:35 by QuadCorei8085, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg401769 - (view) Author: QuadCorei8085 (QuadCorei8085) Date: 2021-09-14 10:35
I'm trying to do something periodically and this would be on the millisec level precision.
However for some reason I could not achieve precise timing in a task.
Below example shows that a simple sleep randomly awakes with a jitter between 0.1-20.0ms I haven't hystogrammed the distribution but it seems to be mostly 19-20ms.

Any way to achieve better timings with asyncio?

async def test_task():
    while True:
        ts_now = time.time();
        await asyncio.sleep(1.000);
        print("{}".format((time.time()-ts_now)*1000.0));

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.create_task(thread_main())
    loop.run_forever()
msg401771 - (view) Author: QuadCorei8085 (QuadCorei8085) Date: 2021-09-14 10:40
correction:
a) of course i meant loop.create_task(test_task())

b) when using threading the sleep returns every 1000ms when going lower 10-15ms is also spot on. (I wanted to use threading in my app but one library - websocket - is asyncio based thus i'm stuck with asyncio)
msg401772 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2021-09-14 10:45
asyncio provides the preemptive concurrency, not the real-time one.
It means that the exact time of task switches depends on other tasks' execution and can vary by design.

Sorry, the jitter is unavoidable.
History
Date User Action Args
2022-04-11 14:59:50adminsetgithub: 89357
2021-09-14 10:45:00asvetlovsetstatus: open -> closed
resolution: wont fix
messages: + msg401772

stage: resolved
2021-09-14 10:40:09QuadCorei8085setmessages: + msg401771
2021-09-14 10:35:13QuadCorei8085create