[Twisted-Python] Re: Re: reactor.callLater ponderings (what i am trying to do) :)

jjanecek at telusplanet.net jjanecek at telusplanet.net
Mon Dec 15 00:15:52 MST 2003


Thanx for your help :)

Here is an example of how i implement LoopClock

from twisted.internet import reactor
print reactor
from twisted.internet.task import LoopingCall
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=10.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
    cur_time=(time.time()-start_time)*1000

    reactor.callInThread(clock_pulse_thread)
    #clock_pulse_thread() 
    clock_count+=frequency
    

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>=10000 :
        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.005)
  
LC=LoopingCall(clock_pulse)
LC.start(frequency/1000.0)

reactor.run()

20 ms on freebsd still seems to be the minimum of resultion 
10 ms on windows and linux :)
anyway 20 ms is adequate.

I would like to help anthony on the shloom project but I have to 
talk to my boss first. Unfortunately I am not the one who makes the
decisions :(. If the small company I am working for goes tits up
then i will release my code as I relocate my office to a dumpster :)

Anyway it is kinda intresting that me and anthony had the idea for a 
VOIP fone using twisted at approx the same time.

I attempting to create an "RTP stack" which will accept and broadcast rtp
packets from a variety of sources and then like filter them down similar
to how a tcp stack works. If that makes sense.

The only problem i had was the timing issue. I tested my code on a 
windoze box, when i moved it up to freebsd box i started to drop packets 
(IE packets where arriving late). I orrigionally thought it was because of
the distance from me to the server (I am like 20 hops). But then I discovered
that the real problem was i was not sending out the packets at the correct time
as I had thought. The class LoopingCall does in a more elegant manner what i was 
attempting to do in my Clocking Program :)


Anyway Thanx for your help, and twisted kiks ass :)






More information about the Twisted-Python mailing list