Python 定时任务


Python 定时任务

创建定时任务

def start_main():
    """编写需要执行的代码"""
    print("执行 开始")

创建时间定时

import datetime
from threading import Timer

from demo import start_main


def func():
    start_main()


def main():
    """
    GET TIME
    now_time: 当前时间
    next_time: 凌晨00:00
    timer_start_time: 凌晨00:00 - 当前时间
    """
    now_time = datetime.datetime.now()
    next_time = now_time + datetime.timedelta(days=1)
    next_year = next_time.date().year
    next_month = next_time.date().month
    next_day = next_time.date().day
    next_time = datetime.datetime.strptime(str(next_year) + "-" + str(next_month) + "-" + str(next_day) + " 00:00:00","%Y-%m-%d %H:%M:%S")
    timer_start_time = (next_time - now_time).total_seconds()
    print("距离启动时间 ", timer_start_time)
    timer = Timer(timer_start_time, func)
    timer.start()


if __name__ == '__main__':
    main()


文章作者: 柒仔
文章链接: /article/41/
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 XiaoLiu!
侵权声明: 若无意对您的文章造成侵权,请您留言,博主看到后会及时处理,谢谢。
评论-----昵称和邮箱必填,网址选填
  目录