Ticket #3229: foo.py

File foo.py, 631 bytes (added by Scramblejams, 7 years ago)
Line 
1from twisted.internet import defer, reactor
2
3NO_OF_DEFERREDS = 332
4
5def do_something(item, d):
6    print item
7    d.callback(item)
8
9def start(items):
10    if items:
11        item = items[0]
12        items = items[1:]
13        d = defer.Deferred()
14        reactor.callLater(0, lambda: do_something(item, d))
15        d.addCallback(lambda _: start(items))
16        return d
17    reactor.stop()
18
19def finished(_):
20    print 'This should print after the numbers are done printing.'
21
22if __name__ == '__main__':
23    items = []
24    for i in range(NO_OF_DEFERREDS):
25        items.append(i)
26    start(items).addCallback(finished)
27    reactor.run()