Using clock() in threading on Windows

Christian Heimes lists at cheimes.de
Fri Feb 20 20:56:36 EST 2009


Maxim Khitrov wrote:
> The threading module uses time.time in _Condition and _Thread classes
> to implement timeouts. On Windows, time() typically has a resolution
> of 15.625ms. In addition, if the system clock is changed (though ntp,
> for example) it would reflect that change, causing the timeout to last
> longer or shorter depending on which way the update went.
> 
> Would it not be better to use time.clock() instead? The resolution is
> much better, and the value is not linked to system clock. Right now, I
> replace the threading._time reference with clock in some of my
> programs and everything works perfectly. Condition and Event timeouts
> are precise down to the millisecond (resolution of the sleep
> function), and I see no side-effects.
> 
> Is it possible to make that change part of the module itself (keeping
> time() for linux systems), or can someone think of a reason why using
> clock is a bad idea? I know that it's using QueryPerformanceCounter
> for implementation, which has some known issues, but I still think
> that the advantages outweigh potential faults.

Can you make a feature request? Your proposal seems sensible. A trivial
patch should solve the issue:

from time import sleep as _sleep
if _sys.name == "win32":
    from time import clock as _time
else:
    from time import time as _time

However I'm not sure about the implications.

Christian




More information about the Python-list mailing list