[Twisted-Python] I could swear I've seen this pattern *somewhere* in Twisted...

Mike C. Fletcher mcfletch at rogers.com
Mon May 9 22:12:29 MDT 2005


James Y Knight wrote:

> How about this? sem.run simplifies things a lot.
> Note also consumeErrors=True to DL.  You probably always want that, 
> as it causes the errors to not get propagated to the toplevel error 
> printer, but rather only reported by the DL.
>
> def parallel( iterable, count, callable, *args, **named ):
>     sem = defer.DeferredSemaphore(count)
>     dl = [sem.run(callable, item, *args, **named) for item in iterable]
>     return defer.DeferredList(dl, consumeErrors=True)

Well, guess that gets it down to the level where it's no longer
necessary to have it wrapped in a function.  That's great.

I didn't even think to look at the base classes when I was reading the
code for DeferredSemaphore.  This is why reference documents are good
things :) .

BTW, anyone know what all this funky stuff in the definition of run is for?

    def run(*args, **kwargs):
        ...
        if len(args) < 2:
            if not args:
                raise TypeError("run() takes at least 2 arguments, none
given.")
            raise TypeError("%s.run() takes at least 2 arguments, 1
given" % (
                args[0].__class__.__name__,))
        self, f = args[:2]
        args = args[2:]

why not just:

    def run( self, f, *args, **kwargs ):

?

Thanks,
Mike

-- 
________________________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://www.vrplumber.com
  http://blog.vrplumber.com





More information about the Twisted-Python mailing list