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.

Title: [Windows] time.sleep() should use CREATE_WAITABLE_TIMER_HIGH_RESOLUTION
Type: enhancement Stage: resolved
Components: Library (Lib), Windows Versions: Python 3.11
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Livius, corona10, eryksun, lukasz.langa, paul.moore, steve.dower, tim.golden, vstinner, zach.ware
Priority: normal Keywords: patch

Created on 2021-10-11 08:28 by vstinner, last changed 2022-04-11 14:59 by admin. This issue is now closed.

File name Uploaded Description Edit corona10, 2021-10-24 16:03
Pull Requests
URL Status Linked Edit
PR 29203 merged corona10, 2021-10-24 16:01
PR 29589 merged corona10, 2021-11-17 06:44
Messages (9)
msg403631 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-10-11 08:28
In bpo-21302, the Windows implementation of time.sleep() was modified to use a waitable timer:

New changeset 58f8adfda3c2b42f654a55500e8e3a6433cb95f2 by Victor Stinner in branch 'main':
bpo-21302: time.sleep() uses waitable timer on Windows (GH-28483)

It now calls the functions:

* CreateWaitableTimerW()
* SetWaitableTimer()
* WaitForMultipleObjects()

While SetWaitableTimer() has a resolution of 100 ns, the timer has a resolution of 15.6 ms in practice.

We could use the undocumented CREATE_WAITABLE_TIMER_HIGH_RESOLUTION flag with CreateWaitableTimerEx(). See:

See also:

msg403634 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-10-11 08:32
See also:
msg403635 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-10-11 08:38
The Go programming language called timeBeginPeriod(1) to get more accurate timers. With the following change, it can now use a high resolution timer (CREATE_WAITABLE_TIMER_HIGH_RESOLUTION) to sleep:
msg403659 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-10-11 15:59
See also bpo-19007: "precise time.time() under Windows 8: use GetSystemTimePreciseAsFileTime".
msg403703 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021-10-11 23:26
It's up to the core devs whether or not Python should try to use a high-resolution timer, which is currently undocumented in the Windows API and implemented only in recent releases of Windows 10 and 11. But if this does get supported, the code should fall back on creating a normal timer if CREATE_WAITABLE_TIMER_HIGH_RESOLUTION makes the call fail. For example:


        LARGE_INTEGER relative_timeout;
        // No need to check for integer overflow, both types are signed
        assert(sizeof(relative_timeout) == sizeof(timeout_100ns));
        // SetWaitableTimerEx(): a negative due time is relative
        relative_timeout.QuadPart = -timeout_100ns;


        HANDLE timer = CreateWaitableTimerExW(NULL, NULL, flags, TIMER_ALL_ACCESS);
        if (timer == NULL)
            if (flags && GetLastError() == ERROR_INVALID_PARAMETER) {
                // CREATE_WAITABLE_TIMER_HIGH_RESOLUTION is not supported.
                flags = 0;
                goto create_timer;
            return -1;

        if (!SetWaitableTimerEx(timer, &relative_timeout,
              0,          // no period; the timer is signaled once
              NULL, NULL, // no completion routine
              NULL,       // no wake context; do not resume from suspend
              0))         // no tolerable delay for timer coalescing
            goto error;
msg404868 - (view) Author: Benjamin Szőke (Livius) * Date: 2021-10-23 12:31
A similar solution was introduced in VirtualBox some months ago. Soon, i could get back my Windows 10 developing PC and i can try this things.
msg404936 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2021-10-24 16:03
average:  0.015609736680984497

average:  2.7387380599975585e-05

Impressive result :)
msg406402 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2021-11-16 13:41
New changeset 55868f1a335cd3853938082a5b25cfba66563135 by Dong-hee Na in branch 'main':
bpo-45429: Support CREATE_WAITABLE_TIMER_HIGH_RESOLUTION if possible (GH-29203)
msg406512 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-11-18 00:27
New changeset fc4474e45eecbea8e88095f28c98c5d56438d841 by Dong-hee Na in branch 'main':
bpo-45429: Merge whatsnew about time.sleep (GH-29589)
Date User Action Args
2022-04-11 14:59:51adminsetgithub: 89592
2021-11-18 06:26:34corona10setstatus: open -> closed
stage: patch review -> resolved
2021-11-18 00:27:03lukasz.langasetnosy: + lukasz.langa
messages: + msg406512
2021-11-17 06:44:09corona10setpull_requests: + pull_request27832
2021-11-16 13:41:28corona10setmessages: + msg406402
2021-10-24 16:03:37corona10setfiles: +

messages: + msg404936
2021-10-24 16:01:39corona10setkeywords: + patch
stage: patch review
pull_requests: + pull_request27472
2021-10-23 12:31:54Liviussetmessages: + msg404868
2021-10-23 12:13:49Liviussetnosy: + Livius
2021-10-11 23:26:20eryksunsetnosy: + eryksun
messages: + msg403703
2021-10-11 15:59:48vstinnersetmessages: + msg403659
2021-10-11 14:03:09corona10setnosy: + corona10
2021-10-11 08:38:54vstinnersetmessages: + msg403635
2021-10-11 08:32:14vstinnersetmessages: + msg403634
2021-10-11 08:29:09vstinnersetnosy: + paul.moore, tim.golden, zach.ware, steve.dower
components: + Windows
2021-10-11 08:28:27vstinnercreate