viff

changeset 998:42eb1cf66ac1

New deep_wait function. This function (it is really a class which acts like a function) takes a value as input and gives a Deferred back. The promise is that the Deferred will only trigger when all Deferreds in the value have been triggered themselves. This happens recursively, meaning that deep_wait will keep waiting until the value if fully evaluated.
author Martin Geisler <mg@daimi.au.dk>
date Tue, 14 Oct 2008 15:06:42 +0200
parents 4a31eebed787
children 23a883d5a4b3
files viff/util.py
diffstat 1 files changed, 25 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- a/viff/util.py	Tue Oct 14 15:05:37 2008 +0200
     1.2 +++ b/viff/util.py	Tue Oct 14 15:06:42 2008 +0200
     1.3 @@ -195,6 +195,31 @@
     1.4      return clone
     1.5  
     1.6  
     1.7 +class deep_wait(Deferred):
     1.8 +
     1.9 +    def __init__(self, result):
    1.10 +        Deferred.__init__(self)
    1.11 +        self._wait(result)
    1.12 +
    1.13 +    def _wait(self, value):
    1.14 +        deferreds = []
    1.15 +
    1.16 +        def collect(value):
    1.17 +            if isinstance(value, Deferred):
    1.18 +                deferreds.append(value)
    1.19 +            if isinstance(value, (tuple, list)):
    1.20 +                map(collect, value)
    1.21 +
    1.22 +        collect(value)
    1.23 +
    1.24 +        if deferreds:
    1.25 +            # There are one or more Deferreds to wait on.
    1.26 +            gatherResults(deferreds).addCallback(self._wait)
    1.27 +        else:
    1.28 +            # Found no Deferreds -- there is nothing to wait on and so
    1.29 +            # we are done!
    1.30 +            self.callback(None)
    1.31 +
    1.32  def find_prime(lower_bound, blum=False):
    1.33      """Find a prime above a lower bound.
    1.34