time module precision

Tim Peters tim.peters at gmail.com
Sun Jan 9 18:59:49 CET 2005

[Tim Peters]
>> Python's time.sleep() calls the Win32 API Sleep() function on
>> Windows. All behavior is inherited from the latter.  See MS's docs:
>> <http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/sleep.asp>

[janeaustine50 at hotmail.com]
> Oh, after a short research, I found that time.sleep uses its customized
> way of sleeping using "select".
> [http://groups.google.com/groups?threadm=ud7i1c9ck.fsf%40ctwd0143.fitlinxx.com]
> So I think its behaviour is more complicated than single MS Sleep call,
> I suppose.

Sorry, the information you found is wrong, as a brief look at Python's
timemodule.c will confirm.  select() is used to implement time.sleep()
on Unixish boxes, but, contrary to the thread you found, it's not
possible to do that on Windows -- the Winsock select() requires at
least one of its socket-set arguments to be non-empty (see MS's
select() docs -- they're telling the truth too <wink>).  Abusing
select() for "short sleeps" is an ugly (but effective) hack limited to
Unixish systems.

> What I want to do is waiting(not busy-delaying) for a few tens to
> hundreds of microseconds in some threads. The closet solution I got is
> using windows QueryPerformanceCounter (in Python, time.clock) with busy
> looping checking if we have past the target time. However, that makes
> the cpu usage upto almost 100%.
> So the problem (waiting tens to hundreds of us without busy looping)
> still remains...

I agree with Peter Hansen's reply here, so please respond to it. 
Windows is not a real-time OS, so what you want to do either cannot be
done on Windows, or you're approaching your actual problem (whatever
that may be) in a way that doesn't make good sense.

More information about the Python-list mailing list