[Twisted-web] using threads in twisted
stephen at thorne.id.au
Fri Apr 15 06:47:25 EDT 2011
On 2011-04-15, Ivan wrote:
> for example, I want to run two tasks ( using thread ), waiting while this tasks is end and after run next several tasks
> How to do this using twisted ? Any example ?
> def task(n):
> sleep n
> 1) 1,2 tasks begin ( thread )
> 2) waiting while all tasks is ends
> 3) ...
> 4) run 3,4,5,6,7 tasks ( thread )
> 5) waiting while all tasks is ends
You can use a combination of deferToThread() and DeferredList() to do
this. It's quite simple:
from twisted.internet import reactor
from twisted.internet import threads
from twisted.internet import defer
print 'I do stuff in threads'
return arg + 1
print 'Starting workers'
task1 = threads.deferToThread(worker, 1)
task2 = threads.deferToThread(worker, 2)
return defer.DeferredList([task1, task2])
print 'result of first round is:', results
l = 
for x in range(10):
print 'result of second round is:', results
d = first_round()
More information about the Twisted-web