我正在使用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服务器钩子:
$vim gunicorn-config.py
import sys
workers = 2
def worker_exit(server, worker):
sys.exit(2)
# server.halt()
但这只是抛出错误并继续产生工人:
# 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个或更多工作程序时,会抛出异常:
[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>
如何在不触发任何异常的情况下退出应用程序.
解决方法:
退出枪口:
sys.exit(4)