U can use sched (Python Built-In Library), but this so simple. therefore we will use APScheduler.

Tutorials : https://apscheduler.readthedocs.io/en/3.x/userguide.html

 

1. Installation

(BlogVenv) C:\Work\Software\eclipse-jee-2019-12-R-win32-x86_64\workspace\blog.daonelab.com>pip list
Package              Version
-------------------- ----------
asgiref              3.2.3
certifi              2019.11.28
chardet              3.0.4
defusedxml           0.6.0
Django               3.0.6
django-allauth       0.41.0
django-debug-toolbar 2.2
django-disqus        0.5
django-js-asset      1.2.2
django-mptt          0.11.0
django-tagging       0.5.0
idna                 2.9
mock                 3.0.5
oauthlib             3.1.0
Pillow               7.0.0
pip                  22.2.2
psycopg2-binary      2.9.3
python3-openid       3.1.0
pytz                 2019.3
requests             2.23.0
requests-oauthlib    1.3.0
setuptools           56.0.0
six                  1.14.0
sqlparse             0.3.0
urllib3              1.25.8
wheel                0.36.2

(BlogVenv) C:\Work\Software\eclipse-jee-2019-12-R-win32-x86_64\workspace\blog.daonelab.com>pip install apscheduler
Collecting apscheduler
  Downloading APScheduler-3.9.1-py2.py3-none-any.whl (59 kB)
     ---------------------------------------- 59.5/59.5 kB ? eta 0:00:00
Requirement already satisfied: setuptools>=0.7 in c:\work\software\eclipse-jee-2019-12-r-win32-x86_64\workspace\blog.daonelab.com\blogvenv\lib\site-packages (from apscheduler) (56.0.0)
Collecting tzlocal!=3.*,>=2.0
  Downloading tzlocal-4.2-py3-none-any.whl (19 kB)
Requirement already satisfied: pytz in c:\work\software\eclipse-jee-2019-12-r-win32-x86_64\workspace\blog.daonelab.com\blogvenv\lib\site-packages (from apscheduler) (2019.3)
Requirement already satisfied: six>=1.4.0 in c:\work\software\eclipse-jee-2019-12-r-win32-x86_64\workspace\blog.daonelab.com\blogvenv\lib\site-packages (from apscheduler) (1.14.0)
Collecting backports.zoneinfo
  Downloading backports.zoneinfo-0.2.1-cp37-cp37m-win_amd64.whl (38 kB)
Collecting tzdata
  Downloading tzdata-2022.2-py2.py3-none-any.whl (336 kB)
     ---------------------------------------- 336.4/336.4 kB 21.7 MB/s eta 0:00:00
Collecting pytz-deprecation-shim
  Downloading pytz_deprecation_shim-0.1.0.post0-py2.py3-none-any.whl (15 kB)
Installing collected packages: tzdata, backports.zoneinfo, pytz-deprecation-shim, tzlocal, apscheduler
Successfully installed apscheduler-3.9.1 backports.zoneinfo-0.2.1 pytz-deprecation-shim-0.1.0.post0 tzdata-2022.2 tzlocal-4.2

(BlogVenv) C:\Work\Software\eclipse-jee-2019-12-R-win32-x86_64\workspace\blog.daonelab.com>pip list
Package               Version
--------------------- -----------
APScheduler           3.9.1 # added
asgiref               3.2.3
backports.zoneinfo    0.2.1 # added
certifi               2019.11.28
chardet               3.0.4
defusedxml            0.6.0
Django                3.0.6
django-allauth        0.41.0
django-debug-toolbar  2.2
django-disqus         0.5
django-js-asset       1.2.2
django-mptt           0.11.0
django-tagging        0.5.0
idna                  2.9
mock                  3.0.5
oauthlib              3.1.0
Pillow                7.0.0
pip                   22.2.2
psycopg2-binary       2.9.3
python3-openid        3.1.0
pytz                  2019.3
pytz-deprecation-shim 0.1.0.post0 # added
requests              2.23.0
requests-oauthlib     1.3.0
setuptools            56.0.0
six                   1.14.0
sqlparse              0.3.0
tzdata                2022.2 # added
tzlocal               4.2 # added
urllib3               1.25.8
wheel                 0.36.2

 

 

2. Don't need add module name in INSTALLED_APPS of settings.py. even no makemigrations & migrate.

 

 

3. Create job App

(BlogVenv) C:\Work\Software\eclipse-jee-2019-12-R-win32-x86_64\workspace\blog.daonelab.com>python manage.py startapp job

 

 

4. settings.py

INSTALLED_APPS = [
...
    'job.apps.JobConfig',
...
]

 

 

5. job/views.py

from django.shortcuts import render
from apscheduler.schedulers.background import BackgroundScheduler

# Create your views here.
class Job():
    
    @staticmethod
    def start():
        def task():
            print("called Job!!")
            
        scheduler = BackgroundScheduler()
        scheduler.add_job(task, "interval", seconds=5)
        scheduler.start()

same blow script

from django.shortcuts import render
from apscheduler.schedulers.background import BackgroundScheduler

# Create your views here.
class Job():
    
    @staticmethod
    def start():
        scheduler = BackgroundScheduler()
        @scheduler.scheduled_job("interval", seconds=5)
        def task():
            print("called Job!!")
            
        scheduler.start()

https://apscheduler.readthedocs.io/en/3.x/modules/triggers/interval.html#module-apscheduler.triggers.interval

 

 

6. job/apps.py

from django.apps import AppConfig


class JobConfig(AppConfig):
    name = "job"

    def ready(self):
        from job.views import Job
        Job.start()

 

 

7. Run server

..
called Job!!
called Job!!
called Job!!
called Job!!
called Job!!
...