apscheduler
Task scheduling library for Python
A Python library for running tasks automatically on schedules or in a queue, supports cron-style timing, fixed intervals, and one-off jobs, and works across multiple servers with persistent storage.
APScheduler (Advanced Python Scheduler) is a task scheduling and task queue library for Python. It allows developers to set up jobs that run automatically at specific times, on repeating schedules, or after fixed intervals, without building that infrastructure from scratch. It can also act as a plain job queue if time-based scheduling is not needed.
The library supports four built-in scheduling patterns. Cron-style scheduling targets specific times or days in a familiar format. Interval-based scheduling runs tasks every fixed number of minutes, hours, or similar units. Calendar-based scheduling runs tasks at intervals measured in months or years, always at the same time of day. One-off scheduling runs a task exactly once at a specific date and time. These patterns can be mixed and matched using combining triggers for more complex needs.
APScheduler works with both traditional thread-based Python code and asynchronous code using asyncio or Trio. It integrates with web applications built on WSGI or ASGI frameworks, making it a practical fit for most Python web stacks.
For applications spread across multiple servers or processes, APScheduler supports persistent storage so schedules and jobs survive process restarts. Storage backends include PostgreSQL, MySQL, SQLite, and MongoDB. When using multiple scheduler or worker instances together, event brokers handle coordination between them. The built-in broker options are PostgreSQL, Redis, and MQTT. Jobs stored in the shared data store are visible to all nodes, allowing both reliability and horizontal scaling.
Extra controls include a cap on how many concurrent copies of the same job can run, a deadline for how late a job is allowed to start before being skipped, and optional jitter (a small random delay added to each job) to avoid all jobs firing at the exact same instant. The current v4 series is a pre-release, and the project advises against using it in production.
Where it fits
- Schedule a Python function to run every night at midnight using cron-style syntax without setting up an external tool like cron or Celery.
- Run background jobs reliably across multiple web servers by storing schedules in PostgreSQL so jobs survive server restarts.
- Add a task queue to a FastAPI or Django app that processes jobs asynchronously using asyncio without a separate broker service.
- Set up a monthly billing job that always fires on the same day of the month regardless of varying month lengths.