flask项目中使用schedule定时任务案例

pip install schedule代码import schedule# 定义定时任务schedule.every().day.at("22:00").do(update_data)schedule.ever

pip install schedule

代码

import schedule
# 定义定时任务
schedule.every().day.at("22:00").do(update_data)
schedule.every().day.at("22:00").do(update_cumulative_data)

# 启动定时任务
def run_scheduler():
    while True:
        schedule.run_pending()
        time.sleep(1)

if __name__ == '__main__':
    # 在单独的线程中运行定时任务
    import threading
    scheduler_thread = threading.Thread(target=run_scheduler)
    scheduler_thread.start()

代码说明
定时任务设置:

使用 schedule.every().day.at(“22:00”) 设置每天晚上 10 点执行任务。

定时任务执行:

使用 schedule.run_pending() 检查并执行到期的任务。

使用 time.sleep(1) 避免 CPU 占用过高。

多线程运行:

使用 threading.Thread 在单独的线程中运行定时任务,避免阻塞 Flask 应用。

发布者:admin,转转请注明出处:http://www.yc00.com/web/1755000480a5225479.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信