Flask-APScheduler
Flask-APScheduler is a Flask extension that integrates the APScheduler library, enabling scheduled tasks within Flask applications. It loads scheduler configurations and job definitions from Flask's settings, provides a REST API for managing jobs, and supports authentication for the API. The library is actively maintained with regular updates, including recent releases to support newer Flask and Python versions.
Warnings
- breaking Flask-APScheduler version 1.13.0 and older versions might not be compatible with Flask 3.x. Version 1.13.1 added explicit support for Flask 3.x.
- breaking Version 1.13.0 dropped support for Python versions older than 3.8 and removed several deprecated methods. Attempting to run on older Python versions or using removed methods will result in errors.
- gotcha Flask-APScheduler explicitly pins APScheduler to version 3.x (e.g., in 1.12.0) to prevent unexpected errors due to significant changes in APScheduler 4.x. Directly installing APScheduler 4.x might cause incompatibilities.
- gotcha When deploying with a WSGI server (like Gunicorn), ensure only one worker process starts the APScheduler instance. APScheduler 3.x is designed to run with a single worker process, and multiple instances can lead to jobs running multiple times or other inconsistencies.
- gotcha If using a persistent jobstore (e.g., SQLAlchemyJobStore), do not register jobs from configuration files (e.g., `app.config`). These jobs should be registered using decorators (`@scheduler.task`) or via the `scheduler.add_job()` method to avoid duplication on application restart.
- gotcha If your scheduled jobs need to interact with the Flask application context (e.g., accessing `current_app`, `Flask-SQLAlchemy`'s `db` object), you must explicitly wrap the context-dependent operations within `with scheduler.app.app_context():`.
Install
-
pip install flask-apscheduler
Imports
- APScheduler
from flask_apscheduler import APScheduler
Quickstart
from flask import Flask
from flask_apscheduler import APScheduler
import os
app = Flask(__name__)
class Config:
SCHEDULER_API_ENABLED = True
# Example job store - use an appropriate one for production
# SCHEDULER_JOBSTORES = {
# 'default': {'type': 'sqlalchemy', 'url': 'sqlite:///jobs.sqlite'}
# }
# Example job execution (APScheduler default is 'threadpool')
# SCHEDULER_EXECUTORS = {
# 'default': {'type': 'threadpool', 'max_workers': 20}
# }
app.config.from_object(Config())
scheduler = APScheduler()
scheduler.init_app(app)
scheduler.start()
# Define a simple job using the decorator
@scheduler.task('interval', id='my_interval_job', seconds=5, misfire_grace_time=900)
def job_function():
print(f"Hello from scheduled job! Time: {scheduler.app.config.get('SCHEDULER_API_ENABLED')}")
@app.route('/')
def index():
return "Flask-APScheduler is running! Check console for job output."
if __name__ == '__main__':
# In development, you might need to handle the reloader carefully.
# For simple cases, `use_reloader=False` or specific deployment setup is needed.
# For production, use a WSGI server (e.g., Gunicorn) and ensure only one worker starts the scheduler.
app.run(debug=True, use_reloader=False)