DEV Community

Cover image for A Simple Scheduler in Python
Bhupesh Varshney 👾
Bhupesh Varshney 👾

Posted on • Updated on • Originally published at

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 😉)

Let's start around by playing with this


First things first let us install the python package first

pip install schedule  
Enter fullscreen mode Exit fullscreen mode


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  

while True:  
Enter fullscreen mode Exit fullscreen mode

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


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)
    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) would give an error
    instead use

    schedule.every(2), 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

Other Variations

import schedule
import time

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

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

def job3():

# some other variations 

while True:
Enter fullscreen mode Exit fullscreen mode

Above are some other ways through which we can schedule jobs

  • schedule.every()

    This executes the job() function every hour

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

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

  • schedule.every()"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)

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

Bonus Stuff


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

def sendEmail(sender_email, password, to, subject, msg):
        server = smtplib.SMTP('', 587)
        server.login(sender_email, password)

        message = f'From: {sender_email}\nTo: {to}\nSubject: {subject}\n\n{msg}'

        server.sendmail(sender_email, to, message)
        print("Email Sent")
        print("Some Error Occured")

if __name__ == '__main__':
    PASSWORD = "password"
    TO = ""
    SUBJECT = "Just having fun"
    MESSAGE = "hey dawg! it's my first Email"

Enter fullscreen mode Exit fullscreen mode

Now go check your spam folder 😜

I hope you liked this post 😄

Seems interesting?, Subscribe 🚀 to read more such cool stuff or just connect with me on Twitter.

Top comments (6)

jairajsahgal profile image

Mea toh tujhe insaan samajta tha, tu toh bhagwan nikla re
Deva re Deva
( I used to think you were human, but you found out to be god himself, oh lord oh lord )
Now I can schedule my selenium program for classes and can sleep peacefully.
I was unable to do because of crontab.
Thank you

bhupesh profile image
Bhupesh Varshney 👾

Glad I was able to help. thanks for reading

anuragrana profile image
Anurag Rana

Great article.

We can trick Gmail to not send the emails in the spam folder by including few keywords like [urgent, important, password reset, otp, bank balance, resume, appointment, interview call etc] of size 0px if the content of the email is HTML.

jadedanial profile image
Jade Danial

Great! I can use this in my personal project.

Does it not consume a lots of RAM?

maniflames profile image

Hey that's pretty nice. Can you also run it in the background and get notified in the main when the job is done?

bhupesh profile image
Bhupesh Varshney 👾

Yeah sure one can run the scheduler in background by running it in a separate thread