Eryk Sun <eryk...@gmail.com> added the comment:

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.

----------
nosy: +eryksun
resolution:  -> third party
type:  -> behavior

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue44681>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to