You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2023/4/12 15:15
# @Author : old tom
# @File : schedule.py
# @Project : futool-tiny-datahub
# @Desc : 定时任务配置
from apscheduler . triggers . cron import CronTrigger
from apscheduler . schedulers . background import BackgroundScheduler
# 后台指定调度, 配合fastapi使用
# BlockingScheduler: 调用start函数后会阻塞当前线程。当调度器是你应用中唯一要运行的东西时( 如上例) 使用。
# BackgroundScheduler: 调用start后主线程不会阻塞。当你不运行任何其他框架时使用, 并希望调度器在你应用的后台执行。
# 每隔 1分钟 运行一次 job 方法
# scheduler.add_job(tick, trigger=CronExpTrigger.parse_crontab('0/1 * * * * * *'), kwargs={
# "name": "bob"
# }
scheduler = BackgroundScheduler ( )
class CronExpTrigger ( CronTrigger ) :
"""
重写cron触发器,支持6 \7 位 cron表达式
7位: * * * * * * *
秒、分、时、天、月、周、年
"""
@classmethod
def parse_crontab ( cls , expr , timezone = None ) :
values = expr . split ( )
if len ( values ) == 6 :
# 6位转7位-->? 转 *-->末尾补充 *
values [ 5 ] = ' * '
values = values + [ ' * ' ]
if len ( values ) != 7 :
raise ValueError ( ' Wrong number of fields; got {} , expected 7 ' . format ( len ( values ) ) )
return cls ( second = values [ 0 ] , minute = values [ 1 ] , hour = values [ 2 ] , day = values [ 3 ] , month = values [ 4 ] ,
day_of_week = values [ 5 ] , year = values [ 6 ] , timezone = timezone )