[python-win32] sleep() for less than .001s?

Tim Roberts timr at probo.com
Fri Aug 4 19:29:10 CEST 2006


Ray Schumacher wrote:

> I have been trying to use sleep() and kernel32.QueryPerformanceCounter
> together; I want to delay until a particular tick without trying up
> the CPU badly.
> However, while time.sleep(.001) relieves the CPU, it has wildly
> unpredictable delay, and sleep(.0001) delays almost nothing at all!
> (I'm watching the parallel port on a scope).
> If I use a while and just count ticks with QueryPerformanceCounter(),
> it is very stable and as desired - but it uses 100% CPU.
>
> RTAI LINUX has a nanosleep() function that does it all very nicely on
> our Debian install, is there a parallel in Win32?
> The MS Sleep() function is in integer milliseconds...


No.  Windows simply has no mechanism for doing sub-millisecond sleeps. 
The default scheduling interval in XP is 10 milliseconds.  You can't get
an interrupt more granular than that, even in kernel mode.  It is
possible to change the scheduling interval to 1 millisecond by using
timeBeginTime, although performance suffers.

It is possible to use QueryPerformanceCounter to do a spin-stall, but
you are essentially in an infinite loop.  ALSO remember that you are
still sharing the CPU with other processes.  If another process decides
it wants to hog the CPU for its whole time slice, you'll lose 10
milliseconds.  If a driver decides it needs to do a thermal
recalibration on your disk drive and stalls for 5 milliseconds, you'll
lose 5 milliseconds.

Remember that RTAI LINUX is a real-time operating system designed for
deterministic processing.  Windows most definitely is not.

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-win32 mailing list