PyEugene

Schedule Module

Python job scheduling for humans.

An in-process scheduler for periodic jobs that uses the builder pattern for configuration. Schedule lets you run Python functions (or any other callable) periodically at pre-determined intervals using a simple, human-friendly syntax.

  • Powerful and human-friendly syntax
  • Easy to test
  • Visibility
  • No difference between scheduler environment and one-off / test environment
  • Encourage use of a queueing system rather than doing the work directly in the scheduler
  • Scales without use of locks

Wishlist for Cron

Installation

pip install schedule

import schedule
import time

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

schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
schedule.every().monday.do(job)
schedule.every().wednesday.at("13:15").do(job)
schedule.every().minute.at(":17").do(job)

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

INTERVALS FOR SCHEDULE.EVERY()

 

  • monday
  • tuesday
  • wednesday
  • thursday
  • friday
  • saturday
  • sunday
  • second
  • seconds
  • minute
  • minutes
  • hour
  • hours
  • day
  • days
  • week
  • weeks

So that is great if you want to run a single job. But what if you want to run multiple jobs?

By default, schedule executes all jobs serially. The reasoning behind this is that it would be difficult to find a model for parallel execution that makes everyone happy.

You can work around this restriction by running each of the jobs in its own thread:

import threading
import time
import schedule

def job():
    print("I'm running on thread %s"

%threading.current_thread())


def run_threaded(job_func):
    job_thread = threading.Thread(target=job_func)
    job_thread.start()

schedule.every(10).seconds.do(run_threaded, job)
schedule.every(10).seconds.do(run_threaded, job)
schedule.every(10).seconds.do(run_threaded, job)
schedule.every(10).seconds.do(run_threaded, job)
schedule.every(10).seconds.do(run_threaded, job)

 

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

def job_that_executes_once():
    # Do some work ...
    return schedule.CancelJob

schedule.every().day.at('22:30').do(job_that_executes_once)

So what if you want the ability to run a job once?

def greet(name):
    print('Hello {}'.format(name))

schedule.every().day.do(greet, 'Andrea').tag('daily-tasks', 'friend')
schedule.every().hour.do(greet, 'John').tag('hourly-tasks', 'friend')
schedule.every().hour.do(greet, 'Monica').tag('hourly-tasks', 'customer')
schedule.every().day.do(greet, 'Derek').tag('daily-tasks', 'guest')

schedule.clear('daily-tasks')

LINKS

schedule-module-pyeugene

By sdudenhofer

schedule-module-pyeugene

A quick walk-though of using the schedule module

  • 39