[Twisted-Python] help with Deferreds

Sergey Magafurov smagafurov at naumen.ru
Thu Aug 26 06:49:25 EDT 2010


> If you don't like that construct, perhaps this one is nicer:
>
> from twisted.internet.defer import maybeDeferred
>
> def getCachedResult(cache, key):
>     def _readItem(item):
>         if item.obsolete():
>             return None
>         return item.read()
>
>     def _gotResult(item):
>         def _release(ign):
>             item.release()
>             return item
>
>         d = maybeDeferred(_readItem, item)
>         d.addBoth(_release)
>         d.addCallback(renew)
>         return d
>
>     def _ebNotFound(f):
>         f.trap(NotFound)
>         return None
>
>     return cache.open(key).addCallbacks(_gotResult, _ebNotFound
Thanks, this is what I was looking for
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20100826/5891151b/attachment-0001.htm 


More information about the Twisted-Python mailing list