gpt4 book ai didi

python - Flask 应用程序退出时如何停止 Gunicorn

转载 作者:IT王子 更新时间:2023-10-29 01:12:52 60 4
gpt4 key购买 nike

我正在使用 gunicorn 运行我的 Flask 应用程序,但是当 Flask 应用程序因错误退出时,gunicorn 将创建一个新的 worker 而不是退出。

示例 Flask 应用程序:

$ vim app.py

# main file
import sys
import os
import logging
from flask import Flask

from views import views

def create_app():

app = Flask(__name__)

app_name = os.environ.get('FLASK_APP_NAME', None)
if app_name is None:
logging.error("Failed to load configuration")
sys.exit(2)

app.config['APP_NAME'] = app_name

console = logging.StreamHandler(sys.stdout)
logging.getLogger().addHandler(console)
logging.getLogger().setLevel(logging.INFO)

logging.info("Starting Flask application")

app.register_blueprint(views)

return app

app = create_app()

if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000, debug=False, threaded=True)


$ vim views.py

# views
from flask import request, jsonify, Blueprint
from flask import current_app as app

views = Blueprint('views', __name__)

@views.route("/", methods=["GET"])
def indexz():
return jsonify(app=app.config['APP_NAME'], msg='OK'), 200

我尝试使用 gunicorn 的 worker_exit 服务器钩子(Hook):

$ vim gunicorn-config.py

import sys

workers = 2

def worker_exit(server, worker):
sys.exit(2)
# server.halt()

但这只是抛出一个错误并继续产生 worker :

# error when using sys.exit(2)
[2016-12-10 01:28:53 +0000] [11916] [INFO] Booting worker with pid: 11916
ERROR:root:Failed to load configuration
[2016-12-10 01:28:53 +0000] [11915] [INFO] Worker exiting (pid: 11915)
[2016-12-10 01:28:53 +0000] [11915] [WARNING] Exception during worker exit:
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/gunicorn/arbiter.py", line 576, in spawn_worker
self.cfg.worker_exit(self, worker)
File "gunicorn-config.py", line 6, in worker_exit
sys.exit(2)
SystemExit: 2


# error when using server.halt()
[2016-12-10 01:30:15 +0000] [12202] [INFO] Booting worker with pid: 12202
ERROR:root:Failed to load configuration
[2016-12-10 01:30:15 +0000] [12197] [INFO] Worker exiting (pid: 12197)
[2016-12-10 01:30:15 +0000] [12197] [INFO] Shutting down: Master
[2016-12-10 01:30:15 +0000] [12197] [WARNING] Exception during worker exit:
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/gunicorn/arbiter.py", line 576, in spawn_worker
self.cfg.worker_exit(self, worker)
File "gunicorn-config.py", line 7, in worker_exit
server.halt()
File "/usr/lib/python2.7/site-packages/gunicorn/arbiter.py", line 331, in halt
self.stop()
File "/usr/lib/python2.7/site-packages/gunicorn/arbiter.py", line 378, in stop
self.kill_workers(sig)
File "/usr/lib/python2.7/site-packages/gunicorn/arbiter.py", line 600, in kill_workers
self.kill_worker(pid, sig)
File "/usr/lib/python2.7/site-packages/gunicorn/arbiter.py", line 616, in kill_worker
self.cfg.worker_exit(self, worker)
File "gunicorn-config.py", line 7, in worker_exit
server.halt()
File "/usr/lib/python2.7/site-packages/gunicorn/arbiter.py", line 338, in halt
sys.exit(exit_status)
SystemExit: 0

编辑:根据 https://github.com/benoitc/gunicorn/blob/master/gunicorn/arbiter.py#L37 将我的应用程序的退出代码修改为:sys.exit(4) .这次应用程序不会无限重启,但是当有 2 个或更多 worker 时,会抛出异常:

[2016-12-10 18:45:52 +0000] [22195] [INFO] Worker exiting (pid: 22195)
Traceback (most recent call last):
File "/usr/bin/gunicorn", line 11, in <module>
sys.exit(run())
File "/usr/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 74, in run
WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
File "/usr/lib/python2.7/site-packages/gunicorn/app/base.py", line 192, in run
super(Application, self).run()
File "/usr/lib/python2.7/site-packages/gunicorn/app/base.py", line 72, in run
Arbiter(self).run()
File "/usr/lib/python2.7/site-packages/gunicorn/arbiter.py", line 218, in run
self.halt(reason=inst.reason, exit_status=inst.exit_status)
File "/usr/lib/python2.7/site-packages/gunicorn/arbiter.py", line 331, in halt
self.stop()
File "/usr/lib/python2.7/site-packages/gunicorn/arbiter.py", line 381, in stop
time.sleep(0.1)
File "/usr/lib/python2.7/site-packages/gunicorn/arbiter.py", line 231, in handle_chld
self.reap_workers()
File "/usr/lib/python2.7/site-packages/gunicorn/arbiter.py", line 506, in reap_workers
raise HaltServer(reason, self.WORKER_BOOT_ERROR)
gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>

如何在不触发任何异常的情况下退出应用程序。

最佳答案

这会退出 gunicorn:

sys.exit(4)

关于python - Flask 应用程序退出时如何停止 Gunicorn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41071262/

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