[Twisted-Python] thread pool question

Manlio Perillo manlio_perillo at libero.it
Thu Mar 22 15:09:07 EDT 2007


Rutt, Benjamin ha scritto:
>  From 
> _http://twistedmatrix.com/projects/core/documentation/howto/threading.html_: 
> 
> 
> I copied the code below and ran it as:
> 
>       $ cat twisted-threadpool.py
>       #!/usr/bin/env python
> 
>       from twisted.internet import reactor
> 
>       def aSillyBlockingMethod(x):
>           import time
>           time.sleep(2)
>           print x
> 
>       # run method in thread
>       reactor.callInThread(aSillyBlockingMethod, "2 seconds have passed")
> 
>       $ ./twisted-threadpool.py
>       2 seconds have passed
> 
> But I note that the program never stops. 

You should use deferToThread.

from twisted.internet import threads

d = threads.deferToThread(aSillyBlockingMethod, "2 seconds have passed")
d.addBoth(reactor.stop)



Regards  Manlio Perillo




More information about the Twisted-Python mailing list