1、 pip install django-crontab 2、 settings.py INSTALLED_APPS => INSTALLED_APPS = ( 'django_crontab', ... ) 3、编写函数 3.1、new file under appname, named cron.py 3.2、add the following codes in settings.py CRONJOBS = [ ('*/5 * * * *', 'myapp.cron.my_scheduled_job') #see more in https://crontab.guru/examples.html ] 3.3、 coding in cron.py def my_scheduled_job(): pass 3.4、python manage.py crontab add other: #显示当前的定时任务 python manage.py crontab show #删除所有定时任务 python manage.py crontab remove
1、pip install apscheduler 2、appname/jobs.py from apscheduler.schedulers.background import BackgroundScheduler from django_apscheduler.jobstores import DjangoJobStore, register_events, register_job import random scheduler = BackgroundScheduler() scheduler.add_jobstore(DjangoJobStore(), "default_cls") try: @register_job(scheduler, 'cron', day_of_week='mon-sun', hour='1', minute='30', second='00', id='clsredis') def time_task(): print(1) register_events(scheduler) scheduler.start() # scheduler.remove_job(time_task) # 移除定时任务 except Exception as e: print(e) scheduler.shutdown() # 在任意VIEW文件引用该jobs文件
django 定时任务 django-crontab & APScheduler 使用
猜你喜欢
转载自blog.csdn.net/qq_36336522/article/details/105690332
今日推荐
周排行