[Twisted-Python] stop conditions and deferToThread()

Thomas Jakobsen thomas.jakobsen at alexandra.dk
Wed Jun 10 06:04:16 EDT 2009


As discussed in a previous thread


a task put in its own thread via deferToThread() won't stop even
though the reactor has stopped. It has to carry out its own check that
the reactor is running and stop if that is not the case.

My question is: Is it possible to use anything else than the
reactor.running as stop condition in this way? In my current twisted
application I would like to keep the reactor running but still have
some deferToThread jobs stop if e.g. an exception occurs in one of the

In the code example below I try to use my own global field, running,
as stop condition, but it doesn't work. If I let bar() call
reactor.stop() after the time.sleep(3) and use reactor.running in
foo() it works, however.

import time
from twisted.internet import reactor
from twisted.internet.threads import deferToThread

running = True

def foo():
   while running:
       print "working"

def bar():
   print "stopping"
   running = False

d1 = deferToThread(foo)
d2 = deferToThread(bar)


Best regards,
Thomas Jakobsen

More information about the Twisted-Python mailing list