您的位置:首页 > 其它

用twisted为未来安排任务(Scheduling tasks for the future

2012-06-04 13:52 423 查看
我们想再x秒后执行一个任务,可以使用twisted.internet.interfaces.IReactorTime:

from twisted.internet import reactor

def f(s):

print "this will run 3.5 seconds after it was scheduled: %s" % s

reactor.callLater(3.5, f, "hello, world")

如果想每x秒就重复执行一个任务,可以使用twisted.internet.task.LoopingCall:

from twisted.internet import task

def runEverySecond():

print "a second has passed"

l = task.LoopingCall(runEverySecond)

l.start(1.0) # call every second

# l.stop() will stop the looping calls

如果想要取消一个已经安排的任务:

from twisted.internet import reactor

def f():

print "I'll never run."

callID = reactor.callLater(5, f)

callID.cancel()

翻译 -- Jerry Marx.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: