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.
You would need to install Django Background Task via
pipon 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
INSTALLED_APPS = [# Other apps...'background_task',]
Create a new file called
tasks.py(or use any name you want to) within one of your Django apps. This guide would usetasks.pywhich will have the scheduled task code.In the
tasks.pyfile, 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 secondsdef my_scheduled_task():# Your task code goes hereprint("Executing scheduled task now...")# This function will be executed at the scheduled time
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_taskfunction in a view function or in a method of your app'sAppConfigclass.
# Import the task function from tasks.pyfrom .tasks import my_scheduled_task# Register the task to be executeddef register_task():# Schedule the task to run every day at 8:00 AMmy_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.
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.