[Twisted-Python] help with Deferreds

Sergey Magafurov smagafurov at naumen.ru
Thu Aug 26 06:51:21 EDT 2010


> Yes, I was expecting you to wrap item.obsolete() and renew(data) in
> try/except, as needed.  If you want a clean but slightly slower way to do
> that, how about something like:
>
>
> def getCachedResult(cache, key):
>
>      def release(result, item):
>          item.release()
>          return result
>
>      def renew(data):
>          return renew(data)
>
>      def notFound(fail):
>          fail.trap(NotFound)
>
>      def readItem(item):
>          return item.read()
>
>      def checkObsolete(item):
>          d = defer.maybeDeferred(item.obsolete)
>          d.addCallback(lambda _: readItem(item))
>          d.addBoth(release, item)
>          d.addCallback(renew)
>          return d
>
>      d = cache.open(key)
>      d.addCallbacks(checkObsolete, notFound)
>      return d
>
>    
Thanks, maybeDeferred is what I missed :)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20100826/25e4c987/attachment.htm 


More information about the Twisted-Python mailing list