hutool.cron.cron¶
定时任务工具类
Module Contents¶
Classes¶
内部Cron任务 |
|
内部固定频率任务 |
|
定时任务工具类 |
API¶
- class hutool.cron.cron._CronTask(pattern: hutool.cron.cron_pattern.CronPattern, func: Callable)¶
内部Cron任务
Initialization
- class hutool.cron.cron._FixedRateTask(func: Callable, period_seconds: int)¶
内部固定频率任务
Initialization
- class hutool.cron.cron.CronUtil¶
定时任务工具类
- _tasks: List[hutool.cron.cron._CronTask] = []¶
- _fixed_tasks: List[hutool.cron.cron._FixedRateTask] = []¶
- _task_ids: Dict[str, int] = None¶
- _running: bool = False¶
- _thread: Optional[threading.Thread] = None¶
- _lock = 'Lock(...)'¶
- classmethod schedule(cron_pattern: str, func: Callable) None¶
添加定时任务。
- 参数:
cron_pattern – cron表达式字符串,格式为 “分 时 日 月 周”
func – 定时执行的函数
- classmethod schedule_at_fixed_rate(func: Callable, period_seconds: int) None¶
添加固定频率任务。
- 参数:
func – 定时执行的函数
period_seconds – 执行间隔(秒)
- classmethod start() None¶
启动调度器。
调度器在后台线程中运行,每秒检查一次是否有需要执行的任务。
- classmethod stop() None¶
停止调度器。
- classmethod restart() None¶
重启调度器。
- classmethod _run_loop() None¶
调度器主循环
- classmethod schedule_with_id(task_id: str, cron_pattern: str, func: Callable) None¶
添加带ID的定时任务
- 参数:
task_id – 任务ID
cron_pattern – cron表达式
func – 执行函数
- classmethod remove(task_id: str) bool¶
移除指定ID的任务
- 参数:
task_id – 任务ID
- 返回:
是否成功移除
- classmethod get_task_count() int¶
获取任务数量
- 返回:
任务总数
- classmethod clear() None¶
清空所有任务
- classmethod matched_dates(cron_pattern: str, start, end) list¶
获取指定时间范围内匹配cron表达式的日期列表
- 参数:
cron_pattern – cron表达式
start – 开始时间(datetime)
end – 结束时间(datetime)
- 返回:
匹配的datetime列表
- classmethod set_cron_setting(setting_path: str) None¶
加载 Cron 配置文件。
配置文件每行格式为
cron_pattern=task_name(暂仅记录路径)。- 参数:
setting_path – 配置文件路径
- classmethod update_pattern(task_index: int, new_pattern: str) None¶
更新已有任务的 Cron 表达式。
- 参数:
task_index – 任务索引(从 0 开始)
new_pattern – 新的 cron 表达式
- 抛出:
IndexError – 索引越界时
ValueError – 表达式不合法时