怎么让定时器每隔一段时间就运行一次,而不是只运行一次?

158 views
Skip to first unread message

Samuel

unread,
Jan 24, 2008, 2:24:39 AM1/24/08
to python-cn:CPyUG
看了threading.Timer和sched.scheduler的手册,都是运行一次后就停掉了,而达不到我要的效果.

zuggie

unread,
Jan 24, 2008, 2:34:17 AM1/24/08
to pyth...@googlegroups.com
from time import *
 
sleep(1 * 10)  ## 每10秒運行一次

2008/1/24 Samuel <PrinceOfD...@gmail.com>:
看了threading.Timer和sched.scheduler的手册,都是运行一次后就停掉了,而达不到我要的效果.



Joseph qu

unread,
Jan 24, 2008, 2:34:20 AM1/24/08
to pyth...@googlegroups.com
每次停了,重新生成定时器

2008/1/24 Samuel <PrinceOfD...@gmail.com>:
看了threading.Timer和sched.scheduler的手册,都是运行一次后就停掉了,而达不到我要的效果.





--
-To learn, To deliver, To communicate

Joseph Qu

xunSir

unread,
Jan 24, 2008, 3:41:18 AM1/24/08
to python-cn:CPyUG
from threading import Timer
def timer_data2txt()
.........................
Timer(24 * 3600, timer_data2txt).start()

Oyster

unread,
Jan 24, 2008, 4:05:40 AM1/24/08
to python-cn:CPyUG
http://code.google.com/p/pyevent/

On 1月24日, 下午3时24分, Samuel <PrinceOfDataMin...@gmail.com> wrote:
> 看了threading.Timer和sched.scheduler的手册,都是运行一次后就停掉了,而达不到我要的效果.

Samuel Chi

unread,
Jan 24, 2008, 11:16:16 AM1/24/08
to pyth...@googlegroups.com
event完美得解决了我的问题.
ps:这真是好东西,太赞了.

在08-1-24,Oyster <lepto....@gmail.com> 写道:

DDGG

unread,
Jan 27, 2008, 4:43:33 AM1/27/08
to python-cn:CPyUG
最好是看一下threading.Timer的实现,然后自己改写个。
下面的例子供参考:

class RepeatableTimer(object):
def __init__(self, interval, function, args=[], kwargs={}):
self.interval = interval
self.function = function
self.args = args
self.kwargs = kwargs

def start(self):
self.stop()
import threading
self._timer = threading.Timer(self.interval, self._run)
self._timer.setDaemon(True)
self._timer.start()

def restart(self):
self.start()

def stop(self):
if self.__dict__.has_key("_timer"):
self._timer.cancel()
del self._timer

def _run(self):
try:
self.function(*self.args, **self.kwargs)
except:
pass
self.restart()
Reply all
Reply to author
Forward
0 new messages