A simple and powerful task scheduling framework with web UI
Project description
Task Schedule
一个简单而强大的Python定时任务调度框架,支持装饰器定义任务、Web界面管理、数据库持久化存储。
功能特性
- 装饰器定义任务: 使用
@scheduled_task装饰器轻松定义定时任务 - 灵活调度: 支持Cron表达式和固定间隔两种调度方式
- Web管理界面: 直观的Web界面查看任务状态和运行日志
- 实时日志: 支持查看任务的实时运行日志
- 数据库存储: 使用SQLite和SQLAlchemy持久化任务信息
- 日志管理: 使用loguru记录任务日志,自动归档和清理
- 易于部署: 支持打包发布到PyPI
安装
pip install task_schedule
快速开始
1. 创建任务文件
在 tasks 目录下创建任务文件:
# tasks/my_tasks.py
from task_schedule import scheduled_task
@scheduled_task(
task_id="my_task",
name="我的任务",
description="这是一个示例任务",
schedule_type="cron",
schedule_value="* * * * *"
)
def my_task():
"""任务函数"""
print("任务执行中...")
return {"status": "success"}
2. 启动框架
# main.py
from task_schedule import main
if __name__ == "__main__":
main(host="0.0.0.0", port=8080)
或者使用命令行:
python main.py
3. 访问Web界面
打开浏览器访问 http://localhost:8080
任务配置
Cron表达式
@scheduled_task(
task_id="daily_task",
name="每日任务",
schedule_type="cron",
schedule_value="0 2 * * *" # 每天凌晨2点执行
)
def daily_task():
pass
固定间隔
@scheduled_task(
task_id="interval_task",
name="间隔任务",
schedule_type="interval",
schedule_value="60" # 每60秒执行
)
def interval_task():
pass
项目结构
task_schedule/
├── task_schedule/ # 主包
│ ├── core/ # 核心模块
│ │ ├── scheduler.py # 任务调度器
│ │ ├── decorator.py # 任务装饰器
│ │ ├── scanner.py # 任务扫描器
│ │ ├── database.py # 数据库模型
│ │ └── logger.py # 日志管理
│ ├── web/ # Web模块
│ │ ├── routes.py # 路由定义
│ │ └── ...
│ └── templates/ # HTML模板
│ ├── index.html
│ ├── task_detail.html
│ ├── task_history.html
│ └── run_detail.html
├── tasks/ # 任务目录
│ ├── __init__.py
│ └── example_tasks.py
├── logs/ # 日志目录
├── pyproject.toml
└── README.md
API接口
GET /- 首页,任务列表GET /task/<task_id>- 任务详情GET /task/<task_id>/history- 任务运行历史GET /task/<task_id>/run/<instance_id>- 运行记录详情GET /api/tasks- 获取所有任务GET /api/tasks/<task_id>- 获取任务详情GET /api/tasks/<task_id>/runs- 获取任务运行记录POST /api/tasks/<task_id>/trigger- 手动触发任务POST /api/tasks/<task_id>/pause- 暂停任务POST /api/tasks/<task_id>/resume- 恢复任务
打包发布
# 构建包
python -m build
# 发布到PyPI
twine upload dist/*
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
task_schedule-1.0.0.tar.gz
(18.0 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file task_schedule-1.0.0.tar.gz.
File metadata
- Download URL: task_schedule-1.0.0.tar.gz
- Upload date:
- Size: 18.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0af3a603a16eef7037741960c460f7a0d1c14d04b2df46566f5df0dce86933d9
|
|
| MD5 |
fdb8abc0aacf127de5d2aabdbfc30b6a
|
|
| BLAKE2b-256 |
c9c479e1522e2a2b40cefec77e011e064046d317302ddd2e0f0e77b8b5e76e1f
|
File details
Details for the file task_schedule-1.0.0-py3-none-any.whl.
File metadata
- Download URL: task_schedule-1.0.0-py3-none-any.whl
- Upload date:
- Size: 22.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9e1c0c829f68ddf96fe6f8b0df8cedcf3f9dce5c3bc2bde582e79d85cf0c2730
|
|
| MD5 |
deb8a9f8242ab09c6ef0d2c7d80ca6d6
|
|
| BLAKE2b-256 |
05779bf3f732f7cf7b0fd28dad837026f1918ccc34e7f8f1a562c2a3d110bb01
|