Flask-APScheduler

1.13.1 · active · verified Tue Apr 14

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize Flask-APScheduler with a Flask application and define a recurring task using a decorator. It includes a basic configuration and ensures the scheduler starts with the application. Note the `use_reloader=False` for development to avoid multiple scheduler instances, which is a common issue.

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)

view raw JSON →