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.
Introduction to the example
In this article, using a simple Flask example, assume that the project structure is as follows:
1 | + app |
Flask applications are started by the app module, and specific scheduled tasks are written in the jobs.py file in the app module.
Install Flask-APScheduler
Execute:
1 | pip install Flask-APScheduler |
Define the scheduled task to perform
In the example, we simply define a method for printing one line of text
1 | def job1(): |
Define scheduling rules
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
1 | from flask import Flask |