Django-apscheduler 定時任功能

github: https://github.com/jarekwg/django-apscheduler.gitpython

# pip install apscheduler

# pip install django-apscheduler

將: django_apscheduler 加到 項目setting.py INSTALLED_APPS 中git

INSTALLED_APPS = ( 
      ... 
      django_apscheduler, 
)

執行:github

# python manage.py migrate

會建立兩張表:django_apscheduler_djangojob/django_apscheduler_djangojobexecution
經過進入後臺管理能方便管理定時任務。django

到任一 view.py 下bash

from apscheduler.schedulers.background import BackgroundScheduler
from django_apscheduler.jobstores import DjangoJobStore, register_events, register_job

scheduler = BackgroundScheduler()
scheduler.add_jobstore(DjangoJobStore(), "default")

scheduler.start()

def time_task(task):
    print("I'm a test job!")

scheduler.add_job(time_task, "cron", id=task.name, hour=hour, minute=minute, second=0,kwargs={"task": task})
register_events(scheduler)

遇到的問題:spa

WARNING base.run_job Line:120 Run time of job "time_task (trigger: cron[hour='11', minute='35', second='0'], next run at: 2018-05-31 11:35:00 CST)" was missed by 0:00:01.115909code

須要加上參數 : misfire_grace_time 默認1s, 儘可能大些ip

scheduler.add_job(time_task, "cron", id=task.name, hour=hour, minute=minute, second=0, misfire_grace_time=30,kwargs={"task": task})
相關文章
相關標籤/搜索