{"id":5934,"library":"flask-apscheduler","title":"Flask-APScheduler","description":"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.","status":"active","version":"1.13.1","language":"en","source_language":"en","source_url":"https://github.com/viniciuschiele/flask-apscheduler","tags":["flask","scheduler","cron","background tasks","job scheduling"],"install":[{"cmd":"pip install flask-apscheduler","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Requires Python 3.8 or higher.","package":"Python","optional":false},{"reason":"Requires Flask 2.2.5 or higher.","package":"Flask","optional":false},{"reason":"Explicitly pinned to version 3.x due to potential breaking changes in APScheduler 4.x.","package":"APScheduler","optional":false}],"imports":[{"symbol":"APScheduler","correct":"from flask_apscheduler import APScheduler"}],"quickstart":{"code":"from flask import Flask\nfrom flask_apscheduler import APScheduler\nimport os\n\napp = Flask(__name__)\n\nclass Config:\n    SCHEDULER_API_ENABLED = True\n    # Example job store - use an appropriate one for production\n    # SCHEDULER_JOBSTORES = {\n    #    'default': {'type': 'sqlalchemy', 'url': 'sqlite:///jobs.sqlite'}\n    # }\n    # Example job execution (APScheduler default is 'threadpool')\n    # SCHEDULER_EXECUTORS = {\n    #    'default': {'type': 'threadpool', 'max_workers': 20}\n    # }\n\napp.config.from_object(Config())\n\nscheduler = APScheduler()\nscheduler.init_app(app)\nscheduler.start()\n\n# Define a simple job using the decorator\n@scheduler.task('interval', id='my_interval_job', seconds=5, misfire_grace_time=900)\ndef job_function():\n    print(f\"Hello from scheduled job! Time: {scheduler.app.config.get('SCHEDULER_API_ENABLED')}\")\n\n@app.route('/')\ndef index():\n    return \"Flask-APScheduler is running! Check console for job output.\"\n\nif __name__ == '__main__':\n    # In development, you might need to handle the reloader carefully.\n    # For simple cases, `use_reloader=False` or specific deployment setup is needed.\n    # For production, use a WSGI server (e.g., Gunicorn) and ensure only one worker starts the scheduler.\n    app.run(debug=True, use_reloader=False)\n","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade to Flask-APScheduler 1.13.1 or newer for Flask 3.x compatibility. Ensure Flask >= 2.2.5 is installed.","message":"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.","severity":"breaking","affected_versions":"<1.13.1"},{"fix":"Ensure your Python environment is 3.8 or newer. Review your code for deprecated methods if upgrading from a significantly older version.","message":"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.","severity":"breaking","affected_versions":"<1.13.0"},{"fix":"Do not manually install APScheduler 4.x if using Flask-APScheduler. Let Flask-APScheduler manage the APScheduler dependency or consult documentation for explicit compatibility.","message":"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.","severity":"gotcha","affected_versions":"All versions that pin APScheduler to 3.x"},{"fix":"Configure your WSGI server to run with a single worker process, or implement logic to ensure `scheduler.start()` is called only once (e.g., in the main process before forking workers, or within a specific worker).","message":"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.","severity":"gotcha","affected_versions":"All versions using APScheduler 3.x"},{"fix":"Register persistent jobs only through decorators or `add_job` calls, not via `SCHEDULER_JOBS` in your Flask configuration.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Wrap Flask context-dependent code inside jobs with `with scheduler.app.app_context():`. Remember to commit database sessions if performing DB operations.","message":"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():`.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}