Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4995947
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类: Python/Ruby

2012-04-23 12:13:57

我们想再x秒后执行一个任务,可以使用twisted.internet.interfaces.IReactorTime:

  1. from twisted.internet import reactor

  2. def f(s):
  3.     print "this will run 3.5 seconds after it was scheduled: %s" % s

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


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

  1. from twisted.internet import task

  2. def runEverySecond():
  3.     print "a second has passed"

  4. l = task.LoopingCall(runEverySecond)
  5. l.start(1.0) # call every second

  6. # l.stop() will stop the looping calls


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

  1. from twisted.internet import reactor

  2. def f():
  3.     print "I'll never run."

  4. callID = reactor.callLater(5, f)
  5. callID.cancel()

 

翻译 -- Jerry Marx.

 
文章来源:

 
阅读(1074) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~