首页 > 解决方案 > 使用 render_template() 时 Gunicorn 缓存 Flask Jinja2 模板

问题描述

到目前为止,我几乎只将 render_template() 用于我的烧瓶应用程序路线。 render_template()直接使用烧瓶时效果很好:

<!-- 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

在每个请求(--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 在渲染时读取文件而无需重新启动工作人员?

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

  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
    

标签: python-3.xflaskjinja2gunicorn

解决方案


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

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

app.run(). 查阅 Flask 文档,看看哪个最适合你。


推荐阅读