在运行 Flask 应用程序后调用函数的正确方法是什么? [英] What's the right approach for calling functions after a flask app is run?

查看:21
本文介绍了在运行 Flask 应用程序后调用函数的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何做一些我认为很简单的事情感到有些困惑.我有一个使用 Flask 编写的简单应用程序.它看起来像这样:

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. 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)

我发现我的终端正在输出 _run_on_start 中的打印语句,但不是其他常用的 Flask 应用程序调试代码.如果我在 app.run 之前删除调用,则输出是正常的.此外,我发现 _run_on_start 的输出在启动时重复两次,但我不知道这是一些奇怪的输出还是该函数实际上被调用了两次.

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. If I remove the call before app.run, the output is normal. 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.

我认为这不是在调用 app.run 之前添加函数调用的正确方法.我查看了 Flask 文档,发现提到了可以使用的各种装饰器,它们允许您在某些请求之前/之后执行函数,但我想在应用服务器运行时执行调用.

I'm assuming this is not the right way to add a function call before you call 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.

此外,我意识到如果我从另一个模块调用这个模块,即不是当 __name__ != "__main__" 我不会接到我对 _run_on_start.

Further, I realise that if I call this module from another module, i.e., not when __name__ != "__main__" my I won't get my call to _run_on_start.

这里的正确方法是什么?在这两种情况下,当我从 CL 和另一个模块开始时?

What's the right approach here? In both cases when I'm starting from the CL and from another module?

推荐答案

重新加载器可以解释函数的重复输出.它做的第一件事是在一个新线程中启动 main 函数,以便它可以监视源文件并在它们发生变化时重新启动线程.使用 use_reloader=False 选项禁用此功能.

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. Disable this with the use_reloader=False option.

如果您希望能够在从不同模块启动服务器时运行您的函数,请将其包装在一个函数中,然后从另一个模块调用该函数:

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.

这篇关于在运行 Flask 应用程序后调用函数的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆