Home  >  Article  >  Backend Development  >  Is there any way to solve the problem of scheduled execution of programs in Python?

Is there any way to solve the problem of scheduled execution of programs in Python?

王林
王林forward
2023-05-08 22:52:061032browse

    Python scheduled execution program (schedule)

    Use the schedule library

    import schedule
    import time
    def job():
        print("I'm working...")
    schedule.every(10).seconds.do(job) # 每10秒执行一次
    schedule.every().wednesday.at("13:15").do(job) # 每周三13点15执行
    schedule.every(10).minutes.do(job) # 每10分钟执行一次
    schedule.every().hour.do(job) # 每小时执行一次
    schedule.every().day.at("10:30").do(job) # 每天十点半执行
    schedule.every(5).to(10).minutes.do(job) # 每5-10分钟随机执行一次
    schedule.every().monday.do(job) # 每周一执行
    schedule.every().minute.at(":17").do(job) # 每分钟的17秒执行一次
    while True:
        schedule.run_pending() # 运行所有可运行的任务

    If you want to execute it at 4:30, you must Write schedule.every().day.at("04:30").do(job)

    instead of just schedule.every().day.at("4:30").do (job)

    Note that if the execution time of the program is shorter than the time interval you set, that is, when the next scheduled program starts to execute, if your last program has not been executed yet, a new process will be started. .

    If there are too many processes, it will be a disaster

    Python scheduled task execution (schedule) time delay pitfall record

    Timed execution of a task through schedule settings

    import schedule
    import time
    # 定义某个任务
    def job():
        ...
    # 设置执行时间
    schedule.every().seconds.do(job) # 每秒执行一次任务
    schedule.every().minutes.do(job) # 每分钟执行一次任务
    schedule.every().hour.do(job) # 每小时执行一次任务
    # 可以指定具体时间间隔
    schedule.every(10).minutes.do(job) # 每10分钟执行一次任务
    # 设置每天某一时刻执行
    schedule.every().day.at("05:15").do(job) # 每天凌晨5点15分执行任务
    # 设置星期几执行
    schedule.every().monday.at("05:15").do(job) # 每周一凌晨5点15分执行任务
    while True:
        schedule.run_pending() # 运行所有可运行的任务

    The pitfall problem

    The first point to note is that you need to manually run the program to start the service (the program will be paused at this time), and then it will continue to execute at the specified time.

    The second point to note is that when using it, due to the program running time, there will be a time delay problem. For example, if the code is set to run every 10 minutes,

    schedule.every(10).minutes.do(job) # 每10分钟执行一次任务

    will result in

    Is there any way to solve the problem of scheduled execution of programs in Python?

    , but the next execution time will be 10 minutes from the end of the last program run. , and what I hope should be that the program runs 10 minutes from the last time, that is, the correct time should be 10:20:21. If this continues, the time delay will become larger and larger.

    Solution

    You only need to add a specific time (the setting here is 15s):

    schedule.every(10).minutes.at(":15").do(job) # 每10分钟过15s执行程序

    In other words, at 10:00:21Start the service,

    The first execution is 10:10:15,
    The second execution is 10:20:15,
    The third execution is 10:30:15,

    This solves the problem of time delay caused by program running time.

    Notice! ! ! When you start the service for the first time, you must run the program 15 seconds after the current time for it to be normal.

    For example, if the service is started at 10:00:08, it will be executed at 10:09:15 next time (exception)

    For example, if the service is started at 10:00:21, it will be executed at 10:00:21 next time Executed at 10:10:15 (normal)

    The above is the detailed content of Is there any way to solve the problem of scheduled execution of programs in Python?. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete