top of page

How to schedule future code execution with Django

If you're building a Django web app and are trying to figure out how to schedule some code to run in the future, you've come to the right place.


Think about now vs. later


Most simple web apps focus on the present. The user provides some data or input and the web app does something with that data and presents something else to the user. But a very common attribute of more complex web apps is making something happen in the future based on something the user does right now.


For example, let's say you're building a web app that notifies users via email when a selected website has been changed. The "right now" piece of that use case is relatively simple: provide users with a form to input their email address and subscribe to updates. The "future" piece is a little bit more complex. We can't assume the user will be logging on to the web app regularly to provide a trigger to check the website for any changes. Instead, we need the web app to create its own trigger. Since we're trying to schedule future code execution with Django, there are a few great ways to achieve this goal.


Multithreading is the key


A novice programmer might suggest the web app immediately enter a loop that continuously checks the time and parses the website for changes at a given interval (and sends an email to the subscribed user if there are any). There are a few problems with this approach. The main problem is that it would break the web app for the current user as soon as they click submit. We can't load a confirmation or home page for the user if we've just entered a loop. The key to solving this problem is multithreading. Basically: kick off a separate thread of execution that handles looping and checking the time, which the current thread of execution continues on its way. Multithreading is a key principle of computer science, and takes a more dominant approach in some programming languages when compared to others. For example, threading is inherent in the native iOS development languages of Objective-C and Swift (they call threads "queues"). Part of the reason for this is multithreading allows developers to make their code more efficient, and efficiency is key when working on platforms with limited resources (like mobile devices). Even though some languages don't emphasize threading due to their use-cases, most languages do have some threading solution. Django is written in Python, and Python does have a few options.


Threading and scheduling in Python


The built-in threading module in Python is a great option. It's intuitive and contains all the features we'll need for this example. All we need to do is create a thread using the Thread constructor, pass in any arguments, and call the start method. The most important argument is the "target", which tells the thread which function to call at first. We can call other functions from this initial one if needed. For our use case of checking a website for changes at a given interval, it would also be useful to add in the schedule module. Instead of writing our own logic in the loop to determine whether we should check the website for changes or not, schedule will handle this for us. This will make our thread more efficient. Alright, enough explaining, let's get to the code!


How to schedule future code execution with Django


import threading
import schedule

def check_website():
    """
    Check a website for any changes!
    """

def schedule_website_check():
    schedule.every().hour.do(check_website)
    while True:
        schedule.run_pending()
        time.sleep(1)
    
thread = threading.Thread(target=schedule_website_check)
thread.start()

Comments


bottom of page