我正在使用gunicorn來運行我的Flask應用程序。我想在應用程序的開始和關閉之前註冊服務器鉤子以執行某些操作,但我很困惑如何將變量傳遞給這些函數以及如何提取其中創建的變量。如何使用在gunicorn的服務器鉤子中創建的變量?
在gunicorn.conf.py:
bind = "0.0.0.0:8000"
workers = 2
loglevel = "info"
preload = True
def on_starting(server):
# register some variables here
print "Starting Flask application"
def on_exit(server):
# perform some clean up tasks here using variables from the application
print "Shutting down Flask application"
在app.py,將試瓶的應用:
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/hello', methods=['POST'])
def hello_world():
return jsonify(message='Hello World')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=9000, debug=False)
運行gunicorn像這樣:$ gunicorn -c gunicorn.conf.py app:app