python中带区间参数的定时器函数

2024-04-19 10:20:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我调用了这个类的静态函数,希望它每3秒运行一次,只运行5次。。但它不是在计数器=5处停止,而是继续运行
我找了又找系统出口(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

Tags: 函数系统defcounter静态计数器itstart