[Twisted-Python] help with Deferreds

Sergey Magafurov smagafurov at naumen.ru
Thu Aug 26 06:23:05 EDT 2010


> I would probably write it something like this:
>
> def getCachedResult(cache, key):
>      def _gotResult(item):
>          try:
>              if item.obsolete():
>                  return None
>              d = item.read()
>              d.addCallback(lambda data: renew(data))
>          except:
>              item.release()
>              raise
>          d.addBoth(lambda ign: item.release())
>          return d
>
>      def _ebNotFound(f):
>          f.trap(NotFound)
>          return None
>
>      return cache.open(key).addCallbacks(_gotResult, _ebNotFound)
>
> Note: code is untested
>    
My variant is like yours.
I ask this question because I am confusing with this ugly slice of code:

try:
     ...
except:
     item.release()
     raise

Only inlineCallbacks helps me...
Thanks.




More information about the Twisted-Python mailing list