1 min read

Python Schedule Library

schedule is a lightweight, in-process scheduler for periodic jobs that uses a human-friendly syntax. It is perfect for simple automation tasks where a full-blown task queue like Celery is overkill.

Installation

pip install schedule

Basic Usage

import schedule
import time

def job():
    print("I'm working...")

# Run every 10 seconds
schedule.every(10).seconds.do(job)

# Run every 10 minutes
schedule.every(10).minutes.do(job)

# Run every hour
schedule.every().hour.do(job)

# Run daily at specific time
schedule.every().day.at("10:30").do(job)

# Run every Monday
schedule.every().monday.do(job)

# Run every Wednesday at 13:15
schedule.every().wednesday.at("13:15").do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

Passing Arguments

You can pass arguments to the job function.

def greet(name):
    print('Hello', name)

schedule.every(10).seconds.do(greet, name='Alice')

Cancelling Jobs

# Tag the job
schedule.every().day.do(job).tag('daily-tasks')

# Cancel by tag
schedule.clear('daily-tasks')

programming/python/python