gre*_*use 3 python multithreading nginx flask uwsgi
大家好,工程师们正在努力改变世界!...以及任何愿意帮助的人:)
在python中添加Runloop代码后,uwsgi似乎需要更长的时间才能杀死
停止uwsgi过去非常快。最近,我集成了一个后台线程来定期检查数据库,并在需要时每60秒进行更改。
这似乎工作得很好,除了现在每次我尝试杀死uwsgi时,都需要很长时间。
################################
## 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)
Run Code Online (Sandbox Code Playgroud)
我用以下命令启动服务器:
$ nginx
Run Code Online (Sandbox Code Playgroud)
然后停止:
$ nginx -s stop
Run Code Online (Sandbox Code Playgroud)
我以以下方式开始uwsgi:
$ uwsgi —enable-threads —ini deploy.ini
Run Code Online (Sandbox Code Playgroud)
我停止uwsgi使用以下命令进行python更改:
ctrl + c (if in the foreground)
Run Code Online (Sandbox Code Playgroud)
否则我用以下命令停止uwsgi:
$ killall -s INT uwsgi
Run Code Online (Sandbox Code Playgroud)
然后在更改python代码后,我再次使用以下命令启动uwsgi:
$ uwsgi —enable-threads —ini deploy.ini
Run Code Online (Sandbox Code Playgroud)
以下是我尝试杀死时的示例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 !!!
Run Code Online (Sandbox Code Playgroud)
任何帮助或提示,我们将不胜感激。请让我知道我是否需要更清楚地了解任何内容或缺少任何细节。
谢谢!埃里克
我知道这个问题有点老了,但是我遇到了同样的问题,谷歌让我到了这里,所以我会为乘坐同一条船到达这里的任何人解答。
该问题似乎是由“ --enable-threads”选项引起的,我们有几个使用uwsgi和flask运行的应用程序,只有带有该选项的应用程序才有问题。
如果要使uwsgi进程快死掉,可以添加以下选项:
reload-mercy = 整数
worker-reload-mercy = 整数
它们将导致uwsgi在int秒后强制退出该过程。
另一方面,如果您只需要重新加载uwsgi,请尝试仅发送SIGHUP信号。这将导致uwsgi重新加载其子级。
POST NOTE:似乎我说的太早了,使用SIGHUP有时也会挂起。我正在使用怜悯选项以避免挂起花费太长时间。
另外,如果有人想关注它,我会在uwsgi github上找到问题报告:
https://github.com/unbit/uwsgi/issues/844
| 归档时间: |
|
| 查看次数: |
2055 次 |
| 最近记录: |