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
d
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
Hi Sergey
> def checkObsolete(item):
> if item.obsolete():
> return None
> else:
> d = item.read()
> d.addBoth(release, item)
> d.addCallback(renew)
> return d
>
> d = cache.open(key)
> d.addCallbacks
On Thu, Aug 26, 2010 at 12:23 PM, Sergey Magafurov wrote:
> I ask this question because I am confusing with this ugly slice of code:
>
> try:
> ...
> except:
> item.release()
> raise
If you don't like that construct, perhaps this one is nicer:
from twisted.internet.defer import maybe
> 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 checkObsolete(item):
> if item.obsolet
> 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))
> exce
Hi Sergey
> How to rewrite this regular function with Deferred-style?
Something like the below (untested) will do the job. You'd have to make
sure that cache.open returned a deferred that fails with NotFound in case
things don't work out.
It would be simpler to translate your code using inlineC
On Thu, Aug 26, 2010 at 11:47 AM, Sergey Magafurov wrote:
> How to rewrite this regular function with Deferred-style?
> Conditions:
> 1. 'cache.open' and 'item.read' become deffered
> 2. 'item.obsolete()' and 'renew(data)' are not deferred
I would probably write it something like this:
def getC
How to rewrite this regular function with Deferred-style?
def getCachedResult(cache, key):
try:
item = cache.open(key)
except NotFound:
return None
try:
if item.obsolete():
return None
data = item.read()
finally:
item.r