Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup APScheduler in a Django project?

Tags:

python

django

Specifically, how to

  • Setup APScheduler in a Django project
  • Start running
  • Write to Django ORM
like image 276
samwize Avatar asked Jul 25 '12 16:07

samwize


People also ask

What is Django scheduler?

Django APScheduler for Scheduler Jobs. Advanced Python Scheduler (APScheduler) is a Python library that lets you schedule your Python code to be executed later, either just once or periodically. You can add new jobs or remove old ones on the fly as you please.

How do I setup Django?

Django can be installed easily using pip . In the command prompt, execute the following command: pip install django . This will download and install Django. After the installation has completed, you can verify your Django installation by executing django-admin --version in the command prompt.


Video Answer


1 Answers

just try below code.

project name: crawling_server

django-admin startproject crawling_server
cd crawling_server

app name : stock_crawling

python manage.py startapp stock_crawling

purpose : print 'hello world' every second

1. install packages

pip install -r requirements.txt
APScheduler==3.8.1
asgiref==3.4.1
backports.zoneinfo==0.2.1
beautifulsoup4==4.10.0
certifi==2021.10.8
Django==3.2.10
django-apscheduler==0.6.0
pytz==2021.3
pytz-deprecation-shim==0.1.0.post0
six==1.16.0
soupsieve==2.3.1
sqlparse==0.4.2
typing_extensions==4.0.1
tzdata==2021.5
tzlocal==4.1

2.

add below code at settings.py

INSTALLED_APPS = [
    ... 
    'stock_crawling',
    'django_apscheduler',
]

APSCHEDULER_DATETIME_FORMAT = "N j, Y, f:s a"  # Default

SCHEDULER_DEFAULT = True

3.

add below code at views.py

import datetime


def send_hello():
    time = datetime.datetime.now()
    print('hello world:[{}]'.format(time))

4

generate operator.py in project dir (see below dir tree)

./
├── crawling_server <- **project dir** 
│   ├── asgi.py
│   ├── operator.py <- **generate this script** 
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── db.sqlite3
├── manage.py
├── requirements.txt
└── stock_crawling 
    ├── admin.py
    ├── apps.py
    ├── migrations
    ├── models.py
    ├── tests.py
    └── views.py

add below code at operator.py

from apscheduler.schedulers.background import BackgroundScheduler
from django_apscheduler.jobstores import register_events, DjangoJobStore
from stock_crawling.views import send_hello


def start():
    scheduler = BackgroundScheduler()
    scheduler.add_jobstore(DjangoJobStore(), 'djangojobstore')
    register_events(scheduler)

    @scheduler.scheduled_job('interval', seconds=1, name='auto_hello')
    def auto_hello():
        send_hello()

    scheduler.start()

Either interval or cron is used as the scheduler trigger. For more information, see the link below.

example interval trigger

@scheduler.scheduled_job('interval', seconds=1, name='auto_hello')

example cron trigger

@scheduler.scheduled_job('cron', hour=1, name='auto_hello')

5.

add below code at app.py in project dir

from django.apps import AppConfig
from django.conf import settings


class StockCrawlingConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'stock_crawling'

    def ready(self):
        if settings.SCHEDULER_DEFAULT:
            from crawling_server import operator
            operator.start()

6. runserver

python manage.py runserver --noreload

7. result

hello world:[2021-12-13 12:34:33.982562]
Job '259a33f9664248bea607aa9ba0cdd587' no longer exists! Skipping logging of job execution...
Job '259a33f9664248bea607aa9ba0cdd587' no longer exists! Skipping logging of job execution...
hello world:[2021-12-13 12:34:34.982743]
Job '259a33f9664248bea607aa9ba0cdd587' no longer exists! Skipping logging of job execution...
Job '259a33f9664248bea607aa9ba0cdd587' no longer exists! Skipping logging of job execution...
hello world:[2021-12-13 12:34:35.986722]
like image 57
Soulduck Avatar answered Oct 15 '22 15:10

Soulduck