[Twisted-Python] Cancel/stop Deferred() at any time

Jean-Paul Calderone exarkun at divmod.com
Tue Jul 14 09:21:40 MDT 2009


On Tue, 14 Jul 2009 11:10:10 -0400, vitaly at synapticvision.com wrote:
>Hello,
>
>is there any way to cancel the Deferred callback in those examples?
>
>1.
>d = defer.Deferred()
>d.addCallback(methodA).addErrback(methodErr)
>d.callback(None)
>
>2.
>d = methodB().addErrback(methodErr)
>wfd = defer.waitForDeferred(d)
>
>
>
>Appreciate any help.
>

Deferred doesn't have any cancellation features.  You can generally achieve
some sort of cancellation by supporting this in the application code creating
and consuming the Deferreds involved.

The details of how to do this depend on what exactly you want to happen when
you "cancel" the operation.

You can find one example of this in the implementation of Twisted's internal
thread-based name resolver:

http://twistedmatrix.com/trac/browser/trunk/twisted/internet/base.py#L193

You can find other examples in the implementation of any other Deferred-
returning API which also supports timeouts, eg the web client:

http://twistedmatrix.com/trac/browser/trunk/twisted/web/client.py#L198

Hope this helps,

Jean-Paul




More information about the Twisted-Python mailing list