[Twisted-Python] inlineCallbacks cascading cancelling and more

Glyph Lefkowitz glyph at twistedmatrix.com
Sat Aug 21 03:06:25 EDT 2010

On Aug 17, 2010, at 12:46 AM, Yaroslav Fedevych wrote:

> On Aug 17, 2010, at 8:49 AM, Sergey Magafurov wrote:
>> ... lots of text skipped ...
> 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, in fact, fail for the most of the time.
> Instead of this, you could try to reconsider the task from the viewpoint of “Let's not add any further callbacks until this is absolutely necessary”. For me, this approach rather works. Moreover, you have the means to do it — a callback can return a Deferred, and so on.

This code doesn't have any tests or documentation, but it's extremely simple, and might give you an idea of how to do something like what you want: <http://divmod.org/trac/browser/trunk/Epsilon/epsilon/pending.py>.

In this case, it's a multi-deferred in the sense that multiple parties might be interested in the initial value of the Deferred; but you could easily create something that would, for example, take a function which takes a Deferred and adds callbacks to it, then call that function to produce a new callback chain each time it happens.

I'm still not completely understanding why you want cancellation to cascade, since cancellation effectively _does_ cascade if you just allow CancellationError to propagate down the callback chains.

Basically, if you can implement a function in terms of functions that take Deferreds, you should really do that, rather than trying to change the way Deferred itself works.

Sorry that I wasn't able to understand your reasons for wanting this Deferred functionality.  It really sounds like Deferred already does all the stuff you want (releasing resources, getting garbage collected, etc) as long as you use them correctly, so I don't know what to suggest you do instead.

More information about the Twisted-Python mailing list