Python  

Python Advanced Schedule library

Here we will discuss the Advanced Python Scheduler library and its features and use cases.Already Task Scheduler  library discussed once. Task schedule 

APScheduler is used for scheduling tasks to run at specific times and intervals. Also, useful to run background tasks and jobs. Also, it's special about multi-scheduling with date-based and event listeners like job adding, executions, and logging or triggering other actions.

Features of APScheduler

  • Good for Persistent job storage databases like SQLite, PostgreSQL, MySQL
  • Supports flexible scheduling, cron-based scheduling
  • Thread-safe to run jobs in threads or processes.
  • Integrated Friendly for Flask, Django, FastAPI, and Any Python application

Core Components

Schedulers

  • Main thread running 'BlockingScheduler' 
  • Background scheduler  runs using 'BackgroundScheduler'

Triggers

  • Run once at a specific time if Jon is scheduled.
  • Triggers run at fixed intervals.
  • Cron expressions run periodically

Job Stores

  • Default Memory and scheduler handle fromPython function
  • Persistent stores jobs in adatabase: SQLAlchemy, MongoDB, Redis, etc.

Installation 

pip install apscheduler

Example

from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.cron import CronTrigger
import time

def my_job():
    print("Job executed!","Runs every 5 seconds")

scheduler = BackgroundScheduler()
scheduler.add_job(my_job, CronTrigger(second="*/5"))  # Runs every 5 seconds
scheduler.start()

try:
    while True:
        time.sleep(1)
except (KeyboardInterrupt, SystemExit):
    scheduler.shutdown()

 Output

Job executed! Runs every 5 seconds
Job executed! Runs every 5 seconds
Job executed! Runs every 5 seconds
Job executed! Runs every 5 seconds