使用 render_template() 时 Gunicorn 缓存 Flask Jinja2 模板

Gunicorn caching Flask Jinja2 templates when using render_template()

到目前为止,我一直在使用 render_template() 几乎专门用于我的烧瓶应用程序路由。 render_template() 直接使用 flask 时效果很好:

<!-- demo_template.html -->
<!doctype html>

<form action="/">
  <label for="name">Name:</label>
  <input type="text" id="name" name="name">
  <input type="submit" value="Submit">
</form>

<title>Hello from Flask</title>
{% if name %}
  <h1>Hello {{ name }}!</h1>
{% else %}
  <h1>Hello, World!</h1>
{% endif %}

from flask import Flask, render_template, request, redirect

app = Flask(__name__)
DEBUG = 1
HOST = '0.0.0.0'
PORT = 8080

def append_to_file(filename, self):
    with open(filename, "a") as text_file:
        text_file.write("\n%s" % self)

@app.route('/')
def hello():
    args_dict = dict(request.args)
    if 'name' in args_dict.keys():
        append_to_file('templates/demo_template.html', '<p>'+args_dict['name']+'</p>')
    return render_template('demo_template.html',**args_dict)

if __name__ == '__main__':
    app.run(debug = DEBUG, host=HOST, port=PORT)

一旦我将 Gunicorn 放在它的前面,基本功能就可以工作,但是附加的内容(名称)在工作程序重新启动之前不会返回。 Gunicorn 似乎缓存了模板。

sudo gunicorn -b 0.0.0.0:8090 app_demo:app -w 1 --log-level=debug --reload

在每次请求后重新启动 worker (--max-requests 1) 似乎重新加载模板并显示附加内容:

sudo gunicorn -b 0.0.0.0:8090 app_demo:app -w 1 --log-level=debug --reload --max-requests 1

这是 Gunicorn 中的错误还是这种行为是预期的。我在 Gunicorn 文档中没有看到任何关于此行为的信息。有没有办法让 gunicorn 在渲染时读取文件而不需要重新启动 worker?

编辑:好的,现在我找到了解决这个问题的两个方法。

  1. 使用 Gunicorns --reload-extra 选项
    • 最快
    sudo gunicorn -b 0.0.0.0:8090 app_demo:app -w 1 --log-level=debug --reload --reload-extra templates/demo_template.html
    
  2. 在 Flask 集合内 app.jinja_env.auto_reload = True
    • 比使用 --max-requests 1 快,比使用 Gunicorn --reload-extra 选项慢
    app = Flask(__name__)
    DEBUG = 1
    HOST = '0.0.0.0'
    PORT = 8080
    app.jinja_env.auto_reload = True
    

这在 Flask 方面可能是 100%。如果您设置了 DEBUG,Flask 不会重新加载模板(除非您设置了新的 TEMPLATES_AUTO_RELOAD 选项)。

当您通过 gunicorn 调用 Flask 时,__name__ == '__main__' 将为 False,因此 app.run() 不会被调用(它由 gunicorn 处理)。这会通过该路径绕过 DEBUG 的设置。

app.run() 之外设置 DEBUG 的方法很少。查阅 Flask 文档,了解哪种最适合您。

  1. 使用 Gunicorns --reload-extra 选项
    • 要查看的模板列表
    sudo gunicorn -b 0.0.0.0:8090 app_demo:app -w 1 --log-level=debug --reload --reload-extra templates/demo_template.html
    
  2. 在 Flask 集合内 app.jinja_env.auto_reload = True
    • 比使用 Gunicorn --reload-extra 选项慢
    app = Flask(__name__)
    DEBUG = 1
    HOST = '0.0.0.0'
    PORT = 8080
    app.jinja_env.auto_reload = True