我正在尝试使用时间表在特定时间在python中运行函数 我尝试过
self.check_run = False
...
def job(self,x):
print("I'm working... {}".format(x))
self.check_run = True
schedule.every().day.at(str(self.mytime)).do(lambda: self.job(count))
while 1:
schedule.run_pending()
time.sleep(1)
if self.check_run:
self.check_run = False
break
此代码工作正常,但我需要在几分钟之内在self.mytime
之前运行另一个函数,并且也在几分钟之内运行self.mytime
之后的另一个函数。
那我该怎么做?
答案 0 :(得分:0)
我已覆盖线程。 Timer
类可访问计时器函数,因此我可以在执行所需的timered
函数之前和之后执行代码。一个小例子,您可以带头做什么:
class MyTimer(threading.Timer):
def run(self):
self.finished.wait(self.interval)
# Here is what will happen BEFORE your code will be executed
if not self.finished.is_set():
self.function(*self.args, **self.kwargs)
self.finished.set()
# Here is what will happen AFTER your code was executed.
将很高兴在需要时提供更多帮助或更好地解释自己。