util: deferredutil: implement our own gatherResults instead of using Twisted's
authorZooko O'Whielacronx <zooko@zooko.com>
Wed, 7 Jan 2009 17:32:07 +0000 (10:32 -0700)
committerZooko O'Whielacronx <zooko@zooko.com>
Wed, 7 Jan 2009 17:32:07 +0000 (10:32 -0700)
Because we want to maintain backwards compatibility to Twisted 2.4.0.

src/allmydata/util/deferredutil.py

index a1767417e58659e4c380b5973999ebf623876ad9..fd6cd0ce16b42ad9d8e77dc92619fe3ee8fc613a 100644 (file)
@@ -21,15 +21,25 @@ def _unwrapFirstError(f):
     f.trap(defer.FirstError)
     raise f.value.subFailure
 
-def gatherResults(deferredList):
-    """Returns list with result of given Deferreds.
-
-    This builds on C{DeferredList} but is useful since you don't
-    need to parse the result for success/failure.
-
-    @type deferredList:  C{list} of L{Deferred}s
-    """
-    d = defer.DeferredList(deferredList, fireOnOneErrback=True, consumeErrors=True)
-    d.addCallbacks(_parseDListResult, _unwrapFirstError)
-    return d
+class ResultsGatherer:
+    def __init__(self, deferredlist):
+        self.deferredlist = deferredlist
+        self.fired = 0
+        self.results = []
+        self.d = defer.Deferred()
+        for d in deferredlist:
+            d.addCallbacks(self._cb, self._eb)
+    def start(self):
+        return self.d
+    def _cb(self, res):
+        self.results.append(res)
+        self.fired += 1
+        if self.fired >= len(self.deferredlist):
+            self.d.callback(self.results)
+    def _eb(self, f):
+        self.d.errback(f)
 
+def gatherResults(deferredlist):
+    """ Return a deferred that fires with a list of the results of the deferreds, or else errbacks with any error. """
+    r = ResultsGatherer(deferredlist)
+    return r.start()