[Twisted-Python] Deferred documentation.

Jonathan Lange jml at mumak.net
Wed Mar 30 11:55:50 EDT 2011


On Wed, Mar 30, 2011 at 4:50 PM, Jasper St. Pierre
<jstpierre at mecheye.net> wrote:
> The problem that I have is that errback flow is awkward... the main
> difference is that addCallbacks will call the errback if its own callback
> fails, right? I can only see that really being useful by accident.
>

Not really.

# 1. Handle error then do the action anyway.
deferred = order_food()
deferred.addErrback(handle_error)
deferred.addCallback(do_action)

# 2. Handle the error but do the action only if
# the error doesn't occur.
deferred = order_food()
deferred.addCallbacks(do_action, handle_error)

# 3. Handle the error for the entire operation.
deferred = order_food()
deferred.addCallback(do_action)
deferred.addErrback(handle_error)

# 4. Do something regardless of success or failure.
deferred = order_food()
deferred.addBoth(do_cleanup)


These are analogous to:

# 1. Handle error then do the action anyway.
try:
    value = order_food()
except:
    handle_error()
do_action(value)

# 2. Handle the error but do the action only if the error doesn't occur.
try:
    value = order_food()
except:
    handle_error()
else:
    do_action(value)

# 3. Handle the error for the entire operation.
try:
    value = order_food()
    do_action(value)
except:
    handle_error()

# 4. Do something regardless of success or failure.
try:
    value = order_food()
finally:
    do_cleanup()



More information about the Twisted-Python mailing list