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

Ray Schumacher rays at blue-cove.com
Fri Aug 4 19:40:01 CEST 2006


In the attached test code, I get - for test in  [0, .1, .01, .001, .00001] seconds 

    # 2.9us hi 5.2us low, 
    # 110ms hi and low, 
    # 16ms hi and low, 
    # 16ms hi and low, 
    # 2.9us hi 5.2us low

Intestingly, if I un-comment the 
    #print '\r',
then the processor usage drops by ~11 per cent.

If anyone has comments on these sleep() results, please enlighten me as to why.
Ideally, on how to implement a shorter sleep() (wrap/weave C?) and still relieve the CPU...

Ray
http://rjs.org
-------------- next part --------------
from ctypes import *
from time import *
#import test1 # to compile it...

import win32process
from sys import argv
import os
    
priorityclasses = [win32process.IDLE_PRIORITY_CLASS,
                   win32process.BELOW_NORMAL_PRIORITY_CLASS,
                   win32process.NORMAL_PRIORITY_CLASS,
                   win32process.ABOVE_NORMAL_PRIORITY_CLASS,
                   win32process.HIGH_PRIORITY_CLASS,
                   win32process.REALTIME_PRIORITY_CLASS]
if len(argv)<3: 
    print 'usage: >>python startApp.py progToRun.py {priority}'
    print 'where priority is in [0:5] of:'
    print priorityclasses
    
def setpriority(pid=None,priority=1):
    """ Set The Priority of a Windows Process.  Priority is a value between 0-5 where
        2 is normal priority.  Default sets the priority of the current
        python process but can take any valid process ID. """
        
    import win32api, win32con
    
    if pid == None:
        pid = win32api.GetCurrentProcessId()
    handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, True, pid)
    win32process.SetPriorityClass(handle, priorityclasses[priority])
    


k32=windll.kernel32
c1 = c_ulonglong()
qPC = k32.QueryPerformanceCounter
qPC(byref(c1))
#print c1.value

priority = priorityclasses[int(argv[2])]
StartupInfo = win32process.STARTUPINFO()
execute_target=None
commandLine='python '+argv[1]+' '+argv[2]+' '+str(c1.value)
processAttributes=None
threadAttributes=None
bInheritHandles=0
dwCreationFlags=priority#|win32process.CREATE_NO_WINDOW ## hide it!
newEnvironment=None

processHndl, primary_thread, pid, tid = win32process.CreateProcess(execute_target,
                           commandLine,
                           processAttributes,
                           threadAttributes,
                           bInheritHandles,
                           dwCreationFlags,
                           newEnvironment,
                           os.getcwd(),
                           StartupInfo)

#print processHndl, primary_thread, pid, tid
-------------- next part --------------
# start with ">>python startapp.py t1.py 5"
print 'test1.py'

from ctypes import *
from time import *
import sys
import os

thisOS = os.name
if thisOS=='nt':
    from dlportio import *
else:
    from parallel import *
 
k32=windll.kernel32
c0 = c_ulonglong()
c1 = c_ulonglong()
qPC = k32.QueryPerformanceCounter
qpFreq = k32.QueryPerformanceFrequency

l=[[]]*1000

for i in range(1000):    
    qPC(byref(c0))
    qPC(byref(c1))
    l[i] = c1.value - c0.value
tickCallTicks = sum(l[40:50])/10.
print 'tick call time', tickCallTicks

t = c_ulonglong()
qpFreq(byref(t))
pFreq = t.value 
print 'processor freq:', pFreq, 'inst./s'
ticksPerUs = pFreq/1000000.

qPC(byref(c0))
sleep(.000001)
qPC(byref(c1))
minSleepTicks = c1.value-c0.value-tickCallTicks
print 'minSleep tics', minSleepTicks
print c1.value-int(sys.argv[1]), 'ticks to start up (',(c1.value-int(sys.argv[1]))/float(pFreq),'s)'

TTLHighDur = ticksPerUs*500 # ~500us
currTicksPerPrd = 10333543

prdN = 0
"""
qPC(byref(c0))
tick0 = c0.value # initial count
nextTTL = tick0
endTick = tick0+pFreq* 15 #seconds
## make a decent TTLOUT
while(True):
    nextTTL = currTicksPerPrd+nextTTL
    #sleep(.001)# sleep does almost nothing?
    while(True): #loop until ~the exact tick
        qPC(byref(c1))
        if c1.value>nextTTL: break
    out(0x378, 0xFF) #set all bits high
    while(True): #loop until ~the exact tick
        qPC(byref(c1))
        if c1.value>nextTTL+TTLHighDur: break
    out(0x378, 0x00) #set all bits low
    prdN += 1
    if c1.value>endTick: break
"""
## test sleep()
test = [0, .1, .01, .001, .00001]
tNum = 0
for slpT in test: 
    # result:
    # 2.9us hi 5.2us low, 
    # 110ms hi and low, 
    # 16ms hi and low, 
    # 16ms hi and low, 
    # 2.9us hi 5.2us low
    print 'testing', slpT
    qPC(byref(c0))
    tick0 = c0.value # initial count
    nextTTL = tick0
    endTick = tick0+pFreq* 8 #seconds
    while(True):
        #print '\r',
        qPC(byref(c0))
        sleep(slpT)
        out(0x378, 0xFF) #set all bits high
        sleep(slpT)
        out(0x378, 0x00) #set all bits low
        prdN += 1
        qPC(byref(c1))
        if c1.value>endTick: break
        #print '\r ', int(.5*(c1.value-c0.value)/float(ticksPerUs)), 'us',
    print '\t', .5*(c1.value-c0.value)/float(ticksPerUs), '\n'
    test[tNum] = .5*(c1.value-c0.value)/float(ticksPerUs)
    tNum += 1
print test



More information about the Python-win32 mailing list