[Twisted-web] using threads in twisted

Stephen Thorne 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

def worker(arg):
    print 'I do stuff in threads'
    return arg + 1

def first_round():
    print 'Starting workers'
    task1 = threads.deferToThread(worker, 1)
    task2 = threads.deferToThread(worker, 2)
    return defer.DeferredList([task1, task2])

def second_round(results):
    print 'result of first round is:', results
    l = []
    for x in range(10):
        l.append(threads.deferToThread(worker, x))
    return defer.DeferredList(l)

def report(results):
    print 'result of second round is:', results

def end(results):
    print results
    reactor.stop()

d = first_round()
d.addCallback(second_round)
d.addCallback(report)
d.addBoth(end)

reactor.run()

-- 
Regards,
Stephen Thorne
Development Engineer
Netbox Blue



More information about the Twisted-web mailing list