python 定时任务执行

最好用的是APScheduler定时框架

可以使用schedule和apschedule模块,其中最好用的是APScheduler定时框架

使用 APScheduler 需要安装

$ pip install apscheduler

首先来看一个周一到周五每天早上6点半定时打印的例子

from apscheduler.schedulers.blocking import BlockingScheduler
from datetime import datetime
# 输出时间
def job():
    print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
# BlockingScheduler
scheduler = BlockingScheduler()
scheduler.add_job(job, 'cron', day_of_week='1-5', hour=6, minute=30)
scheduler.start()

该模块有三种调用方式date, cron和interval

sched.add_job(job, 'interval', seconds=5)
sched.add_job(my_job, 'date', run_date=date(2009, 11, 6), args=['text'])

转自:https://www.cnblogs.com/fengff/p/11011000.html

原文地址:https://www.cnblogs.com/i-shu/p/13157073.html