gpt4 book ai didi

python - uWSGI kill 问题

转载 作者:太空宇宙 更新时间:2023-11-03 13:13:14 32 4
gpt4 key购买 nike

在 Python 中添加一个 runloop 代码后,uWSGI 似乎需要更长的时间来杀死。


设置

  • Python, flask
  • 使用 uWSGI 在 Nginx 上运行
  • 使用 psql 数据库

问题

过去停止 uWSGI 非常快。最近我集成了一个后台线程来定期检查数据库并在需要时每 60 秒进行一次更改。

这似乎工作得很好,除了现在每次我试图杀死 uWSGI 时,都需要很长时间。

  • “似乎”我让服务器运行的时间越长,它死掉的时间就越长,
  • 或者也许它总是在当前 60 秒循环结束后被杀死? (我不确定我的目视检查是否支持这一点)
  • 听起来像是泄漏?

这是我最近添加的代码:

################################
## deploy.ini module .py file ##
################################
from controllers import runloop
from flask import Flask
from flask import request, redirect,Response
app = Flask(__name__)

runloop.startrunloop()

if __name__ == '__main__':
app.run() #app.run(debug=True)

################################
## runloop.py ##
################################

### initialize run loop ###
## code ref: http://stackoverflow.com/a/22900255/2298002
# "Your additional threads must be initiated from the same app that is called by the WSGI server.
# 'The example below creates a background thread that executes every 5 seconds and manipulates data
# structures that are also available to Flask routed functions."
#####################################################################

POOL_TIME = 60 #Seconds

# variables that are accessible from anywhere
commonDataStruct = {}
# lock to control access to variable
dataLock = threading.Lock()
# thread handler
yourThread = threading.Thread()

def startrunloop():
logfuncname = 'runloop.startrunloop'
logging.info(' >> %s >> ENTER ' % logfuncname)

def interrupt():
logging.info(' %s >>>> interrupt() ' % logfuncname)

global yourThread
yourThread.cancel()

def loopfunc():
logging.info(' %s >>> loopfunc() ' % logfuncname)

global commonDataStruct
global yourThread

with dataLock:
# Do your stuff with commonDataStruct Here

# function that performs at most 15 db queries (right now)
# this function will perform many times more db queries in production
auto_close_dws()

# Set the next thread to happen
yourThread = threading.Timer(POOL_TIME, loopfunc, ())
yourThread.start()

def initfunc():
# Do initialisation stuff here
logging.info(' %s >> initfunc() ' % logfuncname)

global yourThread
# Create your thread
yourThread = threading.Timer(POOL_TIME, loopfunc, ())
yourThread.start()

# Initiate
initfunc()
# When you kill Flask (SIGTERM), clear the trigger for the next thread
atexit.register(interrupt)

附加信息(所有 flask 请求都正常工作):

我启动服务器:

$ nginx

并停止:

$ nginx -s stop

我用以下方法启动 uWSGI:

$ uwsgi —enable-threads —ini deploy.ini

我停止 uWSGI 以进行 python 更改:

ctrl + c (if in the foreground)

否则我停止uWSGI:

$ killall -s INT uwsgi

然后在对 Python 代码进行更改后,我再次启动 uWSGI:

$ uwsgi —enable-threads —ini deploy.ini

以下是我尝试终止时 Nginx 输出的示例:

^CSIGINT/SIGQUIT received...killing workers...
Fri May 6 00:50:39 2016 - worker 1 (pid: 49552) is taking too much time to die...NO MERCY !!!
Fri May 6 00:50:39 2016 - worker 2 (pid: 49553) is taking too much time to die...NO MERCY !!!

非常感谢任何帮助或提示。如果我需要更清楚地了解任何事情或遗漏任何细节,请告诉我。

最佳答案

我知道这个问题有点老了,但我遇到了同样的问题,Google 把我带到了这里,所以我会回答任何在同一条船上来到这里的人。

问题似乎是由 --enable-threads 选项引起的,我们有几个运行 uwsgi 和 flask 的应用程序,只有这个选项有问题。

如果你想让 uwsgi 进程死得更快,你可以添加这个选项:

reload-mercy = *int*
worker-reload-mercy = *int*

它们会导致 uwsgi 在 int 秒后强制进程退出。

另一方面,如果您只需要重新加载 uwsgi,请尝试发送一个 SIGHUP 信号。这将导致 uwsgi 重新加载它的 child 。

POST NOTE: 看来我说得太早了,使用 SIGHUP 有时也会挂起。我正在使用怜悯选项来避免挂起时间过长。

此外,我在 uwsgi github 上找到了问题报告,如果有人想关注它:

https://github.com/unbit/uwsgi/issues/844

关于python - uWSGI kill 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37846202/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com