[Twisted-Python] Deferreds

Andrew Bennetts andrew-twisted at puzzling.org
Thu Apr 4 17:06:46 MST 2002


I've been thinking about Deferreds lately, and I think they are lacking
something that I would find very handy.

Chaining success and error callbacks is very handy, but occasionally I find
myself having to do:

    deferred = obj.runAsynchronousThing(...)
    deferred.addCallbacks(success, error)
    # Ensure that we close the objectoid
    deferred.addCallbacks(lambda x, obj=obj: obj.close(),
                          lambda x, obj=obj: obj.close())
    
It'd be nice to have a deferred equivalent of "finally", so that you could
simply do this instead:

    deferred = obj.runAsynchronousThing(...)
    deferred.addCallbacks(success, error)
    # Ensure that we close the objectoid
    deferred.finally(obj.close)

You could of course register multiple final callbacks, all of which take no
arguments.

Would other people find this useful?

On a related note, I recall an IRC discussion where a few of us thought that
Deferred.callback(..) should be Deferred.finish(..) and similarly
.errback(..) should be .fail(..).

-Andrew.





More information about the Twisted-Python mailing list