Python:threading.timer不尊重间隔

DTR*_*DTR 3 python multithreading timer

这是另一个问题的后续问题,我现在已经有了解决方案,但由于不相关的原因,实现似乎没有正常运行。

我有以下代码:

import time
import datetime
import threading

def scheduled_function(cycle):
    cycle += 1
    print "Cycle " + str(cycle) + " complete."
    print "Next cycle at " +  (datetime.datetime.now() + datetime.timedelta(minutes=5)).strftime("%l:%M%p")

    threading.Timer(300, scheduled_function(cycle)).start() # New cycle every 5 mins
    return

scheduled_function(1)

while(True):
    command = raw_input()
    print command
Run Code Online (Sandbox Code Playgroud)

一般来说,这似乎实现了我想要的——允许用户在后台输入命令,同时定期调用函数来执行某种常规活动。然而,间隔(在本例中为 300,应等于 5 分钟)似乎没有执行任何操作,并且程序在一秒钟左右达到最大递归深度。(最大递归对于实际脚本来说不是问题,因为它一次运行的时间可能不会超过几个小时)。

我如何错误地使用 threading.Timer ?

bee*_*ezz 5

那是因为你立即调用它,而不是让它Timer为你调用。

threading.Timer(300, scheduled_function, args=[cycle,]).start()
Run Code Online (Sandbox Code Playgroud)