简体   繁体   English

如何在 flask 中运行 app.run() 之前的代码?

[英]how to run the code before the app.run() in flask?

I'm new on flask.I configured a server with flask+gunicorn.我是 flask 的新手。我用 flask+gunicorn 配置了一个服务器。

the code file called test.py like this:名为test.py的代码文件如下所示:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def test():
    return aa+"world!"

if __name__ == '__main__':
    aa = "hello"
    app.run()

run it using: gunicorn -b 0.0.0.0:8080 test:app运行它使用: gunicorn -b 0.0.0.0:8080 test:app

I got a mistake: NameError: name 'aa' is not defined.报错: NameError: name 'aa' is not defined.

I want some codes like variable aa runing before gunicorn.我想要一些代码,例如在 gunicorn 之前运行的变量aa

How to do that?怎么做?

Put in a small block just before your @app.route and you dont need the last block in the question @app.route之前放一个小块,你不需要问题的最后一个块

 @app.before_first_request
 def _declareStuff():
     global aa
     aa='hello'

Just declare aa outside of " __main__ ", in the global scope of the file. 只需在文件的全局范围内声明“ __main__ ”之外的aa

from flask import Flask
app = Flask(__name__)

@app.route('/')
def test():
    return aa+"world!"

aa = "hello"

if __name__ == '__main__':
    app.run()

The code in the if __name__ == '__main__': block executes only if the Python code is run as a script, eg, from the command line. if __name__ == '__main__':块中的代码仅在Python代码作为脚本运行时执行,例如,从命令行运行。 Gunicorn imports the file, so in that case the code in __main__ will not be executed. Gunicorn导入文件,因此在这种情况下, __main__的代码将不会被执行。


Note that if it is your intention to modify the value of aa then different requests can produce different results depending on how many requests each gunicorn worker process has handled. 请注意,如果您打算修改aa的值,则根据每个gunicorn工作进程处理的请求数量,不同的请求可以产生不同的结果。 eg: 例如:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def test():
    global counter
    counter += 1
    return "{} world! {}".format('aa', counter)

counter = 0

if __name__ == '__main__':
    app.run()

Run the above script with more than one worker ( gunicorn -w 2 ... ) and make several requests to the URL. 使用多个worker( gunicorn -w 2 ... )运行上面的脚本,并向URL发出多个请求。 You should see that the counter is not always contiguous. 您应该看到计数器并不总是连续的。

As of Flask 2.2, the @app.before_first_request decorator suggested by Vipluv in their answer is deprecated and will be removed in 2.3.从 Flask 2.2 开始, Vipluv 在其答案中建议@app.before_first_request装饰器已弃用, 并将在 2.3 中删除

Deprecated since version 2.2: Will be removed in Flask 2.3. 2.2 版后已弃用:将在 Flask 2.3 中删除。 Run setup code when creating the application instead.而是在创建应用程序时运行设置代码。

The equivalent can be done by manually pushing the app context , as suggested by Enkum : 正如 Enkum 所建议的那样,可以通过手动推送应用程序上下文来完成等效操作:

# In place of something like this
@app.before_first_request
def create_tables():
    db.create_all()
    ...

# USE THIS INSTEAD
with app.app_context():
    db.create_all()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM