3
我有一個簡單的例子:如何在線程中使用LoopingCall?
from twisted.internet import utils, reactor
from twisted.internet import defer
from twisted.internet import threads
from twisted.internet.task import LoopingCall,deferLater
import time
def test1():
print 'test'
def test2(res):
l = []
for i in xrange(3):
l.append(threads.deferToThread(test4))
return defer.DeferredList(l)
def test3(res):
pass
def test4():
print 'thread start'
time.sleep(10)
print 'thread stop'
def loop():
d = defer.maybeDeferred(test1)
d = d.addCallback(test2)
d.addCallback(test3)
LoopingCall(loop).start(2)
reactor.run()
它的腳本不正確的工作。我想:
1) print 'test'
2) start 3 threads, waiting while all threads stops
3) sleep 2 seconds
4) repeat