Re: [Twisted-Python] help with Deferreds

2010-08-26 Thread Sergey Magafurov
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

Re: [Twisted-Python] help with Deferreds

2010-08-26 Thread Sergey Magafurov
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

Re: [Twisted-Python] help with Deferreds

2010-08-26 Thread Sergey Magafurov
> 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

Re: [Twisted-Python] help with Deferreds

2010-08-26 Thread Sergey Magafurov
> 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

[Twisted-Python] help with Deferreds

2010-08-26 Thread Sergey Magafurov
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

Re: [Twisted-Python] inlineCallbacks cascading cancelling and more

2010-08-24 Thread Sergey Magafurov
> If you can write some unit tests for this, turn the whole thing into a > patch (using svn diff), and attach it to ticket #4632, that'd be great. > > Jean-Paul > done! ___ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedm

Re: [Twisted-Python] inlineCallbacks cascading cancelling and more

2010-08-23 Thread Sergey Magafurov
Thanks to all! Especially to Yaroslav Fedevych who explain me my misstake in my native language :) I was wrong with deferreds usage. Cascading cancelling of inlineCallbacks is still needed to me, but it can be realized with current Deferred API. This way for example: class InlineCallbacksMa

Re: [Twisted-Python] inlineCallbacks cascading cancelling and more

2010-08-17 Thread Sergey Magafurov
You are currently considering your task from the viewpoint “let's make a callback chain for the perfect workflow and alter this chain in case of anything going wrong.” I think the flaw with this approach is that you are trying to make your “ideal” flow work at all in situations where it would

Re: [Twisted-Python] inlineCallbacks cascading cancelling and more

2010-08-16 Thread Sergey Magafurov
Some mistake fixes in my previous message > @inlineCallbacks > def acquire_tts_for(self, call): > tts_connection = yield self.tts.acquire_connection(timeout=10) # > may raise TimeoutError inside > yield call.make_audio_link_with(tts_connection, 'in', timeout=10) > # may raise TimeoutError

Re: [Twisted-Python] inlineCallbacks cascading cancelling and more

2010-08-16 Thread Sergey Magafurov
First, generally: the most important thing to provide here are unit tests with very clear documentation expressing why you would want these things to work. The test code you provided is unclear because it does not succeed or fail, it just does some things, and then does some other things, wi

[Twisted-Python] inlineCallbacks cascading cancelling and more

2010-08-16 Thread Sergey Magafurov
At first, sorry for my bad english I will try to explain idea about Deferreds and inlineCallbacks Wanted features: 1. Ability to delete callbacks (delCallbacks) 2. Automatic cancelling Deferreds if they are not needed any more (no callbacks registered any more, all was deleted) 3. Ability to add/