[Twisted-Python] reactor.callLater ponderings
jjanecek at telusplanet.net
jjanecek at telusplanet.net
Sun Dec 14 18:41:37 MST 2003
I created this little program to test the callLater feacture in
twisted.
from twisted.internet import pollreactor
pollreactor.install()
##try :
## from twisted.internet import win32eventreactor
## win32eventreactor.install()
##except :
## print "can not install win32 reactor"
from twisted.internet import reactor
from twisted.python import threadable
from twisted.internet import threads
threadable.init(1)
reactor.suggestThreadPoolSize(10)
print reactor
import time
#conclusions would be possible to correct so reactor
#fires more accuratly not required since it is pretty close
#for what ever reason reactor can not go finer then 10ms
#on freebsd the min timer frequency is 20.0 ms
frequency=20.0 #time that callback will pulse in ms
clock_count=0
start_time=None
last_tick=0
def clock_pulse() :
global start_time,clock_count,last_tick
if start_time==None :
start_time=time.time()
next_time=frequency/1000.0
#i noticed that the clock needs to be corrected
#to keep proper time over a long period
cur_time=(time.time()-start_time)*1000
if (cur_time-clock_count)>frequency :
print "too slow correction"
next_time=0
elif(clock_count-cur_time)>frequency :
print "too fast correction"
next_time=(frequency/1000.0)*2
#if clock_count>0 :
#last_tick=time.time
reactor.callLater(next_time,clock_pulse)
reactor.callInThread(clock_pulse_thread)
clock_count+=frequency
#clock_pulse_thread()
def clock_pulse_thread() :
global start_time,clock_count,last_tick
cur_time=time.time()
if last_tick>0 :
print "diff ",(cur_time-last_tick)*1000
pass
last_tick=cur_time
if clock_count>=1000 :
stop_time=time.time()
real_time=(stop_time-start_time)*1000
print "Total Time= ",clock_count," ",real_time
print "Difference= ",clock_count-real_time," percent
",((clock_count-real_time)/real_time)*100.0
reactor.stop()
time.sleep(0.01)
reactor.callLater(frequency/1000.0,clock_pulse)
reactor.run()
Now the instresting thing is that is windows the smallest
increment i can call the callback with is like 10 ms.
While on freebsd even using kqueue it is like 20 ms.
I guess you could call that smallest increment like a tick.
ticks in freebsd and on windows are accurate while on linux they
seem to be all over the place, why is this ?
Also is there a place that I can use to set how frequent i want the reactor
to check to see if a callLater function should be run ?
While on Linux (I tested using redhat 9.0 and mandrake 9.2) it does
not seem to be accurate on anything under 1 second. I peeked inside the
basereactor code and it does not seem to shed anylight.
I am running the clock pulse in a thread to simulate that it has to
wait for something.
More information about the Twisted-Python
mailing list