Cover image for A Simple Scheduler in Python

A Simple Scheduler in Python

bhupesh profile image ๐”น๐•™๐•ฆ๐•ก๐•–๐•ค๐•™ ๐•๐•’๐•ฃ๐•ค๐•™๐•Ÿ๐•–๐•ช ๐Ÿ‘พ Updated on ใƒป3 min read

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  


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:  

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

Other Variations

import schedule
import time

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

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

def job3():

# some other variations 

while True:

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


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('smtp.gmail.com', 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__':
    SENDER_EMAIL = "youremail@xyz.com"
    PASSWORD = "password"
    TO = "yourfrnds@email.com"
    SUBJECT = "Just having fun"
    MESSAGE = "hey dawg! it's my first Email"

Now go check your spam folder ๐Ÿ˜œ

I hope you liked this post ๐Ÿ˜„

See more blogs on bhupeshv.me


import antigravity

<div class="ltagtagcontent">

#<a href="/t/tutorial" class="ltag

<div class="ltag
Tutorial is a general purpose tag. We welcome all types of tutorial - code related or not!

It's all about learning, and using tutorials to teach others!

Posted on by:


markdown guide

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.


Great! I can use this in my personal project.

Does it not consume a lots of RAM?


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