好的,所以我正在研究调度程序,我正在考虑类似 timeOut(3,print,'hello') 的东西,它会每三秒打印一次 hello,我尝试了一些方法,但都失败了。也为此使用 time.sleep 不太有效,因为除了一个任务之外,我还需要运行其他任务
编辑:我发现了如何做我需要的事情,很抱歉让我感到困惑,但这确实满足了我的需要,感谢大家回答。
class test:
def __init__(self):
self.objectives = set()
class Objective:
pass
def interval(self,timeout,function,*data):
newObjective = self.Objective()
newObjective.Class = self
newObjective.timeout = time.time()+timeout
newObjective.timer = timeout
newObjective.function = function
newObjective.repeate = True
newObjective.data = data
self.objectives.add(newObjective)
return True
def runObjectives(self):
timeNow = time.time()
for objective in self.objectives:
timeout = objective.timer
if objective.timeout <= timeNow:
objective.function(*objective.data)
if objective.repeate:
objective.timeout = timeNow + timeout
self.main()
else:
self.objectives.remove(objective)
print('removed')
def main(self):
while True:
self.runObjectives()