apscheduler定时任务
分阻塞式和非阻塞式
from apscheduler.schedulers.background import BackgroundScheduler # 非阻塞 from apscheduler.schedulers.blocking import BlockingScheduler # 阻塞 from apscheduler.triggers.cron import CronTrigger from backports import zoneinfo SH = zoneinfo.ZoneInfo("Asia/Shanghai") scheduler = BackgroundScheduler()
scheduler.add_job(func=play_item, trigger=CronTrigger.from_crontab(expr=cron, timezone=SH), id=id, name=name, args=(name,)) # 增加 scheduler.modify_job(job_id=id, func=play_item, trigger=CronTrigger.from_crontab(expr=cron, timezone=SH), name=name, args=(name,)) # 修改 scheduler.remove_job(id) # 删除 job = scheduler.get_job(id) # 查询 # 获取所有任务 jobs = scheduler.get_jobs() print('@' * 30, 'jobs', '@' * 30) print(jobs) # 主线程需要一直运行,否则后台任务无法执行 try: while True: pass except KeyboardInterrupt: scheduler.shutdown()