APScheduler在django中是比较常用的执行定时任务的模块。通过python manage.py runserver 运行时定时任务可以正常运行。使用uwsgi+nginx部署(uwsgi+nginx配置)到线上时APScheduler定时任务没有正常运行。网上搜索解决方法
1.uwsgi配置添加--enable-threads = true,未解决
2.uwsgi配置添加mule = app/__init__.py,uwsgi日志重复刷新,任务也还没有运行
3.在项目的wsgi.py文件导入APScheduler实例化对象,解决
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
vim wsgi.py ... from App.schedulerjob import sched ... #schedulerjob 定时任务的配置文件 vim schedulerjob.py ... def test(): print('ok') # 定时任务 job_defaults = {'max_instances': 5, 'coalesce': True} sched = BackgroundScheduler(job_defaults) sched.add_jobstore(DjangoJobStore(), 'default') sched.add_job(test, 'interval', seconds=60, id='test') sched.start() ... |
APScheduler使用的是BackgroundScheduler模式时,不能使用MemoryJobStore进行存储,会出现脑裂风险