linux如何配置uwsgi定时任,Django APScheduler + uwsgi 定时任务重复运行

基于某些原因可能在开发的时候通过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.

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

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"

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"

解决方法2:

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)

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)

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

分享文章:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值