[Twisted-Python] Twisted CallLater: Advanced Usage

Bob Ippolito bob at redivi.com
Sun Jul 13 16:47:38 EDT 2003


On Sunday, Jul 13, 2003, at 08:24 America/New_York, Moshe Zadka wrote:

> from __future__ import generators
> def os_path_walk(path, visit, arg):
>     d = defer.Deferred()
>     def walk(path):
>         files = os.listdir(path)
>         visit(arg, path, files)
>         for file in files:
>             yield None
>             if os.path.isdir(file):
>                 for _ in walk(os.path.join(path, file):
>                     pass
>     next = walk(path).next
>     def caller():
>         try:
>             next()
>         except StopIteration:
>             d.callback(None)
>         except:
>             d.errback(sys.exc_info()[1])
>         else:
>             reactor.callLater(0, caller)
>     caller()
>     return d

This is close to what I did when I wrote 'flow' (different from cce's), 
except I did the "caller" phase as a function-wrapper (akin to 
staticmethod or classmethod), and I made it smarter, such that it could 
return a value, get the result of a deferred (the next() wouldn't 
happen until the result of the deferred was ready), etc.

AFAIK, since cce's flow is in Twisted now, this example is better 
served using that.  Also, you should be showing people Failure() 
instead of sys.exc_info()[1].

-bob





More information about the Twisted-Python mailing list