loading...

Scheduling tasks using APScheduler in Django

glootieappdev profile image Glootie ・1 min read

This tutorial deals with showing how to schedule tasks using APScheduler in Django and not with real basics of Python or Django.

Okay, let's start

Installing APScheduler:

Run the following command in the terminal:

pip install apscheduler

Setting up APScheduler:

Let's consider the app is named room.

Adding something_update.py to our app directory:

This is how your room/something_update.py should look:

def update_somthing():
    print("this function runs every 10 seconds")

Adding updater.py to our app directory:

This is how your room/updater.py should look:

from apscheduler.schedulers.background import BackgroundScheduler
from .something_update import update_something


def start():
    scheduler = BackgroundScheduler()
    scheduler.add_job(update_something, 'interval', seconds=10)
    scheduler.start()

Starting the Updater:

This is how your room/apps.py should look:

from django.apps import AppConfig


class RoomConfig(AppConfig):
    name = 'room'

    def ready(self):
        from . import updater
        updater.start()

Thank you, that's it for this tutorial.

Posted on by:

glootieappdev profile

Glootie

@glootieappdev

DO NOT DEVELOP MY APP.

Discussion

pic
Editor guide