APScheduler is a flexible, in-process task scheduler library for Python, offering cron-like capabilities. It allows you to schedule Python code to be executed later, either once or periodically, within your application. The current stable version is 3.11.2. It supports various scheduler types, job stores (e.g., in-memory, SQLAlchemy, MongoDB), and triggers (date, interval, cron, calendarinterval). APScheduler is primarily meant to be run inside existing applications, not as a standalone daemon. It is actively maintained with a stable 3.x series and an ongoing pre-release 4.x series with significant architectural changes.
pip install apschedulerVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates a `BackgroundScheduler` which runs jobs in a separate thread without blocking the main application. It schedules a simple function to run every 5 seconds using an `IntervalTrigger`. The `while True: time.sleep(2)` loop keeps the main thread alive, allowing the background scheduler to operate. A `KeyboardInterrupt` handler ensures a graceful shutdown of the scheduler.
Refer to the APScheduler 4.x migration guide before upgrading. A direct migration path for persistent job store data from 3.x to 4.x is not automatically available at the time of writing and may require manual recreation or a specific migration tool when 4.x is stable.
For standalone scripts, use `while True: time.sleep(interval)` with a `try...except KeyboardInterrupt` block, or use `BlockingScheduler` if the scheduler is the only thing running in your process. For web applications, ensure the scheduler's lifecycle is tied to the application's.
Ensure scheduled functions are top-level functions in a module, or static/class methods, and provide their fully qualified path (e.g., `scheduler.add_job('my_module.my_function', ...)`).For multi-process or multi-node deployments, APScheduler 3.x typically requires a single, dedicated scheduler process that manages jobs, with other processes communicating with it. Alternatively, consider using a distributed task queue (like Celery) if strong guarantees and distributed coordination are critical. APScheduler 4.x aims to address this with enhanced data stores and event brokers.
Migrate your timezone definitions to use `zoneinfo` (built-in in Python 3.9+) or `backports.zoneinfo` for older Python versions. For example, `from zoneinfo import ZoneInfo` instead of `from pytz import timezone`.
from apscheduler.schedulers.asyncio import AsyncIOScheduler
import asyncio
Rename the local 'calendar.py' file to avoid name conflicts.
Ensure APScheduler is installed in your current Python environment using `pip install apscheduler`. If using a virtual environment, activate it before installing. If a local file `apscheduler.py` exists, rename it.
Define the job function as a module-level function, a static method, or a class method. Pass a textual reference (e.g., 'your_module.your_function') instead of a direct callable reference to `add_job`.