[Twisted-Python] reactor.stop() and deferToThead

Jean-Paul Calderone exarkun at divmod.com
Fri May 29 08:40:21 EDT 2009

On Fri, 29 May 2009 14:22:43 +0200, Thomas Jakobsen <thomas.jakobsen at alexandra.dk> wrote:
>It seems that things being deferredToThread continue to run even
>though reactor.stop() is called.

Threads cannot be interrupted.  They will run until they function they are
running returns.

>Output from the example below is:
>stuff1 finished; stopping reactor
>stuff2 finished
>Is there a way to abort the remaining execution of stuff2 in this
>case? It would be handy if, say, some exception happens in in stuff1
>that causes the execution of the remaining stuff2 to be meaningless.

You can make stuff2 cooperate with stuff1 so that it returns earlier.

> [snip]
>def stuff1():
>    time.sleep(2)
>    print "stuff1 finished; stopping reactor"
>    reactor.stop()

Also, you're calling reactor.stop() in a non-reactor thread here.  This is
not allowed.  You must call it in the reactor thread, along with almost
every other API.  Try reactor.callFromThread(reactor.stop), instead.


More information about the Twisted-Python mailing list