python在特定时间使用线程运行功能计划

时间:2019-08-21 04:36:33

标签: python datetime time scheduled-tasks datetime-format

我正在尝试使用时间表在特定时间在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之后的另一个函数。

那我该怎么做?

1 个答案:

答案 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.

将很高兴在需要时提供更多帮助或更好地解释自己。