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.

Author eryksun
Recipients eryksun, jack__d, paul.moore, steve.dower, steven.daprano, therenoisfood, tim.golden, zach.ware
Date 2021-07-20.08:56:58
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1626771418.42.0.88180445877.issue44681@roundup.psfhosted.org>
In-reply-to
Content
The implementation of time.sleep() uses WaitForSingleObjectEx() on the main thread. It waits for an event object that gets signaled by Ctrl+C. On other threads it simply calls Sleep(). 

Thread wait functions such as WaitForSingleObjectEx() and Sleep() are based on the system interrupt time. By default the clock interrupt runs at 64 cycles per second, i.e. the interrupt time is about 15.6 ms. The interrupt time can be programmatically lowered to about 0.5 ms, but this should only be changed temporarily for timing critical applications. Lowering the interrupt time for general use can shorten the battery life on portable devices, since servicing the interrupt prevents the CPU from entering a low-power state. 

Even with a lowered interrupt time, in my experience thread dispatching in Windows simply is not implemented to support precise timing. If the wait time needs to be precise, I suggest using a loop based on time.perf_counter_ns(). Calculate a deadline based on the current counter value plus the desired wait time in nanoseconds, and loop until the current value equals or exceeds the deadline. Maybe it would be useful to implement something like this in time.sleep() itself, but I don't know whether the need in a few cases warrants the increased complexity and cost in general.
History
Date User Action Args
2021-07-20 08:56:58eryksunsetrecipients: + eryksun, paul.moore, tim.golden, steven.daprano, zach.ware, steve.dower, jack__d, therenoisfood
2021-07-20 08:56:58eryksunsetmessageid: <1626771418.42.0.88180445877.issue44681@roundup.psfhosted.org>
2021-07-20 08:56:58eryksunlinkissue44681 messages
2021-07-20 08:56:58eryksuncreate