[英]What's the right approach for calling functions after a flask app is run?
I'm a little confused about how to do something that I thought would be quite simple. 我对如何做一些我认为非常简单的事情感到有些困惑。 I have a simple app written using
Flask
. 我有一个使用
Flask
编写的简单应用程序。 It looks something like this: 它看起来像这样:
from flask import Flask
app = Flask(__name__)
def _run_on_start(a_string):
print "doing something important with %s" % a_string
@app.route('/')
def root():
return 'hello world'
if __name__ == "__main__":
if len(sys.argv) < 2:
raise Exception("Must provide domain for application execution.")
else:
DOM = sys.argv[1]
_run_on_start("%s" % DOM)
app.run(debug=True)
What I'm finding is that my terminal is outputting the print statements in _run_on_start
but non of the other usual Flask app debug code. 我发现我的终端输出的是
_run_on_start
的print语句,而不是其他常用的Flask app调试代码。 If I remove the call before app.run, the output is normal. 如果我在app.run之前删除了调用,则输出正常。 Further I'm finding the output of
_run_on_start
to be repeated twice on startup, though I don't know if it's some weird output or the function is actually being called twice. 此外,我发现
_run_on_start
的输出在启动时重复两次,但我不知道它是否是一些奇怪的输出,或者该函数实际上被调用了两次。
I'm assuming this is not the right way to add a function call before you call app.run
. 我假设这不是在调用
app.run
之前添加函数调用的正确方法。 I looked in the Flask docs and found mentions of various decorators one can use, which allow you to execute a function before/after certain requests but I want to execute the call when the app server is run. 我查看了Flask文档并发现了可以使用的各种装饰器的提及,它允许您在某些请求之前/之后执行一个函数,但我想在运行应用服务器时执行调用。
Further, I realise that if I call this module from another module, ie, not when __name__ != "__main__"
my I won't get my call to _run_on_start
. 此外,我意识到,如果我从另一个模块调用此模块,即,当
__name__ != "__main__"
我不会接到我对_run_on_start
调用。
What's the right approach here? 这里有什么正确的方法? In both cases when I'm starting from the CL and from another module?
在这两种情况下,当我从CL和另一个模块开始?
Probably you were looking for Flask.before_first_request
decorator, as in: 可能你正在寻找
Flask.before_first_request
装饰器,如:
@app.before_first_request
def _run_on_start(a_string):
print "doing something important with %s" % a_string
The duplicate output from your function can be explained by the reloader. 重新加载器可以解释函数的重复输出。 The first thing it does is start the main function in a new thread so it can monitor the source files and restart the thread when they change.
它首先要做的是在新线程中启动main函数,以便它可以监视源文件并在更改时重新启动线程。 Disable this with the
use_reloader=False
option. 使用
use_reloader=False
选项禁用它。
If you want to be able to run your function when starting the server from a different module, wrap it in a function, and call that function from the other module: 如果您希望能够在从其他模块启动服务器时运行您的功能,请将其包装在一个函数中,并从另一个模块调用该函数:
def run_server(dom):
_run_on_start("%s" % dom)
app.run(debug=True, use_reloader=False)
if __name__ == '__main__':
if len(sys.argv) < 2:
raise Exception("Must provide domain for application execution.")
else:
DOM = sys.argv[1]
run_server(DOM)
The "right approach" depends on what you're actually trying to accomplish here. “正确的方法”取决于你真正想要在这里完成的事情。 The built-in server is meant for running your application in a local testing environment before deploying it to a production server, so the problem of starting it from a different module doesn't make much sense on its own.
内置服务器用于在将应用程序部署到生产服务器之前在本地测试环境中运行应用程序,因此从不同模块启动它的问题本身并没有多大意义。
from flask import Flask
def create_app():
app = Flask(__name__)
def run_on_start(*args, **argv):
print "function before start"
run_on_start()
return app
app = create_app()
@app.route("/")
def hello():
return "Hello World!"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.