python中带有interval参数的timer函数

时间:2015-06-23 09:07:41

标签: python

我调用了类的静态函数,并且预计它将每3秒运行一次并且只运行5次..但它不会在计数器= 5处停止,继续运行
我搜索并发现sys.exit(0)可以停止计时器。这有什么不对吗?

def senLogtoBackup():
  threading.Timer(3, senLogtoBackup).start()
  AccessLog.AccessLog.backupAccessLog("logbackups","example.log")

try:
    senLogtoBackup()
    if AccessLog.AccessLog.Counter == 5:
        sys.exit(0) # expects I it will terminate the timer. but it still counts

班级定义:

class AccessLog:
        Counter =0
        @staticmethod
        def backupAccessLog(target, source):
            AccessLog.Counter+=1
            print "counter",AccessLog.Counter

1 个答案:

答案 0 :(得分:0)

您搜索错误,请查看os._exit

你应该使用“t.cancel”来停止计时器。