Scheduling scheduled tasks is a common feature in background applications. The APScheduler framework is a framework commonly used in Python to manage and schedule scheduled tasks. The framework is powerful and supports multiple scheduling methods. This article demonstrates how to integrate APSchedule in a Flask web application.
In this article, we use Flask-APScheduler to integrate APScheduler.
In this article, using a simple Flask example, assume that the project structure is as follows:
Flask applications are started by the app module, and specific scheduled tasks are written in the jobs.py file in the app module.
pip install Flask-APScheduler
In the example, we simply define a method for printing one line of text
In the init.py file of the app module, we initialize Flask and start APScheduler. By using the Config object, we define a scheduling rule that defines job1 execution at 6:50 a.m. from Monday to Friday.
Note: The func definition in JOBS must be written to correspond to import * from app.jobs, otherwise it is easy to get the module not found error
from flask import Flask