[Twisted-Python] integrating CompStrm//adding background processing

Bill la Forge laforge49 at yahoo.co.in
Sun May 16 05:49:56 MDT 2004


I've been working on integrating Compstrm 
( http://compstrm.sourceforge.net ) and, while integration was pretty 
easy, it got harder when I wanted to speed things up.
 
Basicly, compstrm uses yields to impliment a kind of light-weight 
threads. So I needed to add background processing to the main reactor 
loop. Here's the code I finally came up with:
 
from twisted.internet import reactor
def _runUntilCurrentNew():
    if reactor.poll:
        p=reactor.poll
        reactor.poll=None
        p()
    _runUntilCurrentOld()
 
_runUntilCurrentOld=reactor.runUntilCurrent
reactor.runUntilCurrent=_runUntilCurrentNew

reactor.poll=None
def _timeoutNew():
    if reactor.poll:
        return 0
    return _timeoutOld
 
_timeoutOld=reactor.timeout
reactor.timeout=_timeoutNew

Just using reactor.callLater, I could only get a speed of 90, in contrast to 
the asyncore integration which was doing better than 12,000.
 
By replacing runUntilCurrent and timeout, I managed to bump my speed up to better than 8,000, which seems reasonable, as Twisted is a bit 
more "heavy weight" than asyncore. ;-)
 
While I'm at it, here's my revised takedown code:

class whenNoDelayedCalls:
    "I check for when there are no delayed calls."
    
    def __init__(self,granularity=1.0,func=reactor.stop):
        self.func=func
        self.granularity=granularity
        reactor.callLater(granularity,self)
    def __call__(self):
        c=len(reactor.getDelayedCalls())
        if c or reactor.poll:
            reactor.callLater(self.granularity,self)
        else:
            self.func()
 
def pollLoop(granularity=1.0,func=reactor.stop):
    "I run the reactor until there are no more delayed calls."
    
    whenNoDelayedCalls(granularity,func)
    reactor.run()

This gives me an approximate equivalent to the asyncore poll loop,
at least when there's no threads or sockets running. ;-)
 


Bill la Forge
http://www.geocities.com/laforge49/
Yahoo! India Matrimony: Find your partner online.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20040516/56a15f73/attachment.html>


More information about the Twisted-Python mailing list