Reputation: 19
I am working on a web app using Django,How to run scheduler peroidically
Basically I just want to run through the database and make some calculations/updates on an automatic, regular basis, but I can't seem to find any documentation on doing this.
How to install or configure it?
To clarify: I know I can set up a cron job to do this, but I'm curious if there is some feature in Django that provides this functionality. I'd like people to be able to deploy this app themselves without having to do much config (preferably zero).
I've considered triggering these actions "retroactively" by simply checking if a job should have been run since the last time a request was sent to the site, but I'm hoping for something a bit cleaner.
Upvotes: 0
Views: 368
Reputation: 4392
at first install apscheduler:
pip install apscheduler
the make a file schedule.py
in your app directory.
then add this code the it:
from datetime import datetime
from apscheduler.schedulers.background import BackgroundScheduler
from any_file import function #import the function that you want
def start():
scheduler = BackgroundScheduler()
scheduler.add_job(your_function, 'interval', minutes=5) #every five minute for example
scheduler.start()
the add this to apps.py
:
from django.apps import AppConfig
class AppConfig(AppConfig):
name = 'app' #your app name
def ready(self):
from your_app import schedule
schedule.start()
then update your INSTALLED_APPS in settings.py.add this to it:
'yourApp.apps.AppConfig' #import class in apps.py
Upvotes: 1