Specifically, how to
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.
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.
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
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
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
add below code at views.py
import datetime
def send_hello():
time = datetime.datetime.now()
print('hello world:[{}]'.format(time))
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')
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()
python manage.py runserver --noreload
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With