A Simple Scheduler in Python

📅️ Published: April 3, 2019  • 🕣 3 min read

A Simple Scheduler in Python

We all encounter things in life which we want to automate, Setting up reminders and schedules are one of them
Python makes it easy for all the developers out there to make small python scripts that can schedule some boring stuff for you.

Here comes this #awesome library called schedule
(quite a name it got there :wink:)

Let’s start around by playing with this

Installation

First things first let us install the python package first

pip install schedule  

Introduction

schedule is 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 predetermined intervals using a simple, human-friendly syntax.

Python job scheduling for humans.

Let’s not worry about what in-process scheduling is for now
Let’s write some code

import schedule  
  
def job():  
    print("A Simple Python Scheduler.")  
  
# run the function job() every 2 seconds  
schedule.every(2).seconds.do(job)  
  
while True:  
    schedule.run_pending()  

The above code prints A Simple Python Scheduler. every 2 seconds.

scheduler

Let’s understand line by line

  • import schedule This needs no explaining just importing the package to use.

  • def job() This is the function which we want to execute according to our schedule.

  • schedule.every(2).seconds.do(job) This is where magic happens
    A job is created and returned by Scheduler.every() method, which also defines its interval (in time units) here the interval is in seconds.
    The do() specifies the job_func that should be called every time the job runs.
    Any additional arguments are passed on to job_func when the job runs.
    *i.e
    the statement schedule.every(2).seconds.do(job(argument)) would give an error instead use
    schedule.every(2).seconds.do(job, arg1, arg2)

  • schedule.run_pending() The run_pending() just runs all jobs that are scheduled to run. Make sure to run it in a loop because so that the scheduling task keeps on running all time.

Hurray we wrote our very first Scheduler using Python hurray

Other Variations

import schedule
import time

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

def job2():
    print("yo boiss..")

def job3():
    print("Hello")
    
schedule.every(5).seconds.do(job)
# some other variations 
schedule.every().hour.do(job)
schedule.every().day.at("12:25").do(job)
schedule.every(5).to(10).minutes.do(job)
schedule.every().thursday.at("19:15").do(job)
schedule.every().wednesday.at("13:15").do(job)
schedule.every().minute.at(":17").do(job)
schedule.every(2).seconds.do(job2)

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

Above are some other ways through which we can schedule jobs

  • schedule.every().hour.do(job)

    This executes the job() function every hour

  • schedule.every().day.at("12:25").do(job)

    This executes the job() function every day at 12:25 PM By default schedule uses 24 hr format.

  • schedule.every().wednesday.at("13:15").do(job)

    Do job() every Wednesday at 1:15 PM. You can also specify day-names to run a particular job. See the list of available ones.

  • schedule.every(2).to(5).minutes.do(job3)

    This one executes job3() every 2 to 5 minutes ;)

Bonus Stuff

yeahh

So now you are able to schedule things What if you could remind yourself of some items to do ??

smtplib comes to the rescue.

Using smtplib you can send emails (emails will come in the spam folder though 🙃)

Here is a simple script to send emails using python

import smtplib
  
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("youremail", "yourpassword")
  
msg = "FIRST EMAIL USING PYTHON!"
server.sendmail("youremail", "email-of-receiver", msg)
server.quit()
print("Email Sent")

Now go check your spam folder 😜

I hope you liked this post :smile: