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

Dan charnedn at muohio.edu
Fri May 29 06:52:19 MDT 2009


Python seems to intentionally make aborting threads nigh impossible. You're
probably way better off doing something akin to this:

    def stuff2():
        time.sleep(4)
        if reactor.running:
            print "stuff2 finished"
        else:
            print "Don't need to stuff2"

Also iirc in Twisted you need to use callFromThread on reactor methods
rather than calling the reactor directly.


On Fri, 29 May 2009 14:22:43 +0200, Thomas Jakobsen
<thomas.jakobsen at alexandra.dk> wrote:
> Hi
> 
> It seems that things being deferredToThread continue to run even
> though reactor.stop() is called.
> 
> 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.
> 
> Kind regards,
> Thomas
> 
> ---------------------------------------------
> 
> from twisted.internet.threads import deferToThread
> from twisted.internet import reactor
> 
> def stuff1():
>     time.sleep(2)
>     print "stuff1 finished; stopping reactor"
>     reactor.stop()
> 
> def stuff2():
>     time.sleep(4)
>     print "stuff2 finished"
> 
> d1 = deferToThread(stuff1)
> d2 = deferToThread(stuff2)
> 
> reactor.run()
> 
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python




More information about the Twisted-Python mailing list