[Twisted-Python] Re: [Twisted-commits] Today is screw with twisted's guts day.

Jp Calderone exarkun at intarweb.us
Wed Nov 19 20:12:56 EST 2003


On Thu, Nov 20, 2003 at 11:53:19AM +1100, Andrew Bennetts wrote:
> [snip]
> 
> So I think it's possible to lose thread calls.  I *think* this patch will
> fix it, but more eyeballs are always welcome with threaded code:

  Thanks for noticing this.

> 
> diff -u -r1.68 base.py
> --- twisted/internet/base.py    19 Nov 2003 21:07:38 -0000      1.68
> +++ twisted/internet/base.py    20 Nov 2003 00:52:08 -0000
> @@ -400,12 +400,14 @@
>          """Run all pending timed calls.
>          """
>          if self.threadCallQueue:
> +            count = 0
>              for (f, a, kw) in self.threadCallQueue:
>                  try:
>                      f(*a, **kw)
>                  except:
>                      log.err()
> -            del self.threadCallQueue[:]
> +                count += 1
> +            del self.threadCallQueue[:count]
>          now = time()
>          while self._pendingTimedCalls and (self._pendingTimedCalls[-1].time <= now):
>              call = self._pendingTimedCalls.pop()
> 

  That looks a little more complex than it needs to be.  What about this
version?


    def runUntilCurrent(self):
        """Run all pending timed calls.
        """
        if self.threadCallQueue:
            calls, self.threadCallQueue = self.threadCallQueue, []
            for (f, a, kw) in calls:               
                try:
                    f(*a, **kw)
                except:
                    log.err()

  I considered doing it this way first, but wasn't sure if anyone relied on
the identity of threadCallQueue (not that anyone should, but one never
knows).  If anyone thinks changing its identity should be avoided, I'd like
this version:

    def runUntilCurrent(self):
        """Run all pending timed calls.
        """
        if self.threadCallQueue:
            calls = self.threadCallQueue[:]
            del self.threadCallQueue[:]
            for (f, a, kw) in calls:
                try:
                    f(*a, **kw)
                except:
                    log.err()


  I suppose reverting it entirely is an option, seeing as how no one has yet
*actually* been bitten by the quadratic behavior (nor does it seem likely
that anyone will be), but all other things equal, it's nicer to use O(N)
algorithms deep down in the guts of Twisted than to use O(N**2) algorithms.

  Jp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20031119/72313699/attachment.pgp 


More information about the Twisted-Python mailing list