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
    评论
Linux系统上部署Django + nginx + uWSGI的步骤如下: 1. 安装必要的软件 在Linux系统上安装必要的软件包,包括Python、pip、nginx、uWSGI等。 2. 创建Django项目 使用Django创建一个新项目或使用现有的Django项目。 3. 配置uWSGIDjango项目的根目录下创建一个uwsgi.ini文件,用于配置uWSGI。示例配置如下: ``` [uwsgi] # 指运行模式为WSGI http = :8000 # 指Django应用的wsgi模块 wsgi-file = myproject.wsgi # 指进程数 processes = 4 # 指线程数 threads = 2 # 指静态文件路径 static-map = /static=/path/to/static # 指日志路径 logto = /path/to/logfile ``` 其中,http参数指了监听的端口号,wsgi-file参数指Django应用的wsgi模块,processes参数指了进程数,threads参数指了线程数,static-map参数指了静态文件的路径,logto参数指了日志文件的路径。 4. 配置nginx 在nginx的配置文件中添加以下内容: ``` server { listen 80; server_name example.com; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; location / { uwsgi_pass 127.0.0.1:8000; include uwsgi_params; } location /static { alias /path/to/static; } } ``` 其中,server_name指了域名,access_log和error_log指了日志文件的路径,uwsgi_pass指uWSGI的地址和端口号,include指uWSGI的参数。 5. 启动uWSGI服务 使用以下命令启动uWSGI服务: ``` uwsgi --ini uwsgi.ini ``` 6. 启动nginx服务 使用以下命令启动nginx服务: ``` sudo service nginx start ``` 这样就完成了Django + nginx + uWSGI的部署。可以通过访问该网站的域名来验证是否部署成功。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值