How to set up a scheduled job in Django

In this Answer we'll look at how to leverage Django background tasks to construct scheduled jobs in Django, which will allow us to automate procedures and streamline backend operations. Besides, Django background tasks, we can use third-party libraries like Celery or APScheduler to create a scheduled Job.

This guide will focus on how to automate processes using Django Background Tasks.

Django background task

Django Background Task is an amazing task-scheduling library that lets developers automate and execute the background operations of a Django application. This helps the developers increase application speed, offload time-consuming activities and assure a smoother user experience as well as run tasks at the scheduled time.

How to use Django background task

Now, that you've understood the importance of the Django background task. Let's learn how to automate one.

  1. You would need to install Django Background Task via pip on your command line. You could also read more about its installation process here. Run this code in your terminal or command prompt.

pip install django-background-tasks
  1. Next, add the installed app to your Django project's INSTALLED_APPS list in the settings.py file.

INSTALLED_APPS = [
# Other apps...
'background_task',
]
  1. Create a new file called tasks.py (or use any name you want to) within one of your Django apps. This guide would use tasks.py which will have the scheduled task code.

  2. In the tasks.py file, define the task's function. The function will run at a specified time. This guide will set the time for 60 seconds.

from background_task import background
@background(schedule=60) # Schedule the task to run every 60 seconds
def my_scheduled_task():
# Your task code goes here
print("Executing scheduled task now...")
# This function will be executed at the scheduled time
  1. Next, register the task function to be executed in the Django background task scheduler. This is done by calling the function in your Django code. For example, you can call my_scheduled_task function in a view function or in a method of your app's AppConfig class.

# Import the task function from tasks.py
from .tasks import my_scheduled_task
# Register the task to be executed
def register_task():
# Schedule the task to run every day at 8:00 AM
my_scheduled_task.schedule(repeat=Task.DAILY, time=datetime.time(hour=8, minute=0))

The snippet above imports the task function from tasks.py and uses the schedule method to repeat the task and specify an execution time. In this guide, the task is scheduled to run every day at 8:00 AM.

  1. Lastly, run this code on the terminal to start the background task:

python manage.py process_tasks

To complete the task, you must start the background task worker. When the specified time arrives, the worker will watch the task and execute it. To keep the task worker running, run this code in a different terminal window.

The output would look like this:

Starting background task worker...
Executing scheduled task now...
Executing scheduled task now...
Executing scheduled task now...
...

There you have it! You have successfully created your first scheduled task which runs at your specified interval.

Conclusion

In conclusion, setting up a scheduled job in Django lets the developers optimize backend functionalities and saves a lot of time. By using this built-in tool, we can automate a recurring task, provide a unique user experience, and enhance application efficiency.

Free Resources