前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Django APScheduler + uwsgi 定时任务重复运行

Django APScheduler + uwsgi 定时任务重复运行

作者头像
obaby
发布2023-02-24 14:56:48
1.3K0
发布2023-02-24 14:56:48
举报
文章被收录于专栏:obaby@mars

基于某些原因可能在开发的时候通过django的manage.py运行定时任务没有任何的问题,但是一旦到了线上环境通过nginx+uwsgi来运行就会发现定时任务不断的重复执行,并且基本都执行失败了。发生这个问题的原因在于uwsgi启动了多个进程来提供服务,于是每次启动的时候定时任务都会跟着再启动一次,于是有4个进程的话,对应的服务就会启动4次,除了第一次可能执行成功后面的基本都会挂掉。

要解决这个问题其实也不难,只要保证在第一次启动的时候添加定时任务并且执行,以后启动的进程不再处理定时任务即可。但是在这种条件下通过python的进程互斥其实貌似并不是非常好使,具体可以看这个:

uWSGI employs some tricks which disable the Global Interpreter Lock and with it, the use of threads which are vital to the operation of APScheduler. To fix this, you need to re-enable the GIL using the --enable-threads switch. See the uWSGI documentation for more details. Also, assuming that you will run more than one worker process (as you typically would in production), you should also read the next section.

https://apscheduler.readthedocs.io/en/latest/faq.html#how-can-i-use-apscheduler-with-uwsgi

基于这个原因其实可以自己来创建相关的互斥,保证只有一个运行即可,解决方法1:

代码语言:javascript
复制
import sys, socket

try:
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(("127.0.0.1", 47200))
except socket.error:
    print "!!!scheduler already started, DO NOTHING"
else:
    from apscheduler.schedulers.background import BackgroundScheduler
    scheduler = BackgroundScheduler()
    scheduler.start()
    print "scheduler started"

https://stackoverflow.com/questions/16053364/make-sure-only-one-worker-launches-the-apscheduler-event-in-a-pyramid-web-app-ru/40162246#40162246

解决方法2:

代码语言:javascript
复制
import atexit
import fcntl
from flask_apscheduler import APScheduler
 
def init(app):
    f = open("scheduler.lock", "wb")
    try:
        fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
        scheduler = APScheduler()
        scheduler.init_app(app)
        scheduler.start()
    except:
        pass
    def unlock():
        fcntl.flock(f, fcntl.LOCK_UN)
        f.close()
    atexit.register(unlock)

https://www.cnblogs.com/shenckicc/p/7019639.html?utm_source=itdadao&utm_medium=referral

解决问题的思想都是一致的,我用的是第一种方法。

☆文章版权声明☆

* 网站名称:obaby@mars * 网址:https://h4ck.org.cn/ * 本文标题: 《Django APScheduler + uwsgi 定时任务重复运行》 * 本文链接:https://h4ck.org.cn/2019/01/django-apscheduler-uwsgi-%e5%ae%9a%e6%97%b6%e4%bb%bb%e5%8a%a1%e9%87%8d%e5%a4%8d%e8%bf%90%e8%a1%8c/ * 转载文章请标明文章来源,原文标题以及原文链接。请遵从 《署名-非商业性使用-相同方式共享 2.5 中国大陆 (CC BY-NC-SA 2.5 CN) 》许可协议。


分享文章:

相关文章:

  1. ngix+uwsgi+django 以及阿里云rds数据库数据导入
  2. ubuntu uwsgi No module named ‘django’
  3. 由apscheduler引发的django.db.utils.InternalError: (1054, u”Unknown column ‘rms.go_datetime’ in ‘field list'”)
  4. django raw_id_fields 显示名称而不是id(raw_id_fields: How to show a name instead of id)
  5. Django 限制访问频率
  6. 再谈《Django 限制访问频率》
  7. Django input value值被截断
  8. Django REST framework foreignkey 序列化
  9. Django admin Foreignkey ManyToMany list_display展示
  10. django 主动抛出 403 异常
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019年1月11日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 相关文章:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档