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

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

问题描述

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

  from flask import Flask 
$ b $ app = Flask(__ name__)

def _run_on_start(a_string):
print用%s做重要的事情%a_string

@ app.route('/')
def root ():
return'hello world'

if __name__ ==__main__:
if len(sys.argv)< 2:
raise Exception(必须为应用程序执行提供域名)
else:
DOM = sys.argv [1]
_run_on_start(%s%DOM)
app.run(debug = True)

我发现我的终端在 _run_on_start 中输出打印语句,但不是其他常用的Flask应用程序调试代码。如果我在app.run之前删除呼叫,则输出正常。进一步,我发现 _run_on_start 的输出要在启动时重复两次,不过我不知道是不是有些奇怪的输出,或者这个函数实际上被调用了两次。 / p>

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



进一步,我意识到,如果我从另一个模块调用这个模块,即不是当 __ name__!=__main __我不会得到我打给 _run_on_start

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

解决方案

您的函数的重复输出可以解释由reloader。它所做的第一件事是在一个新线程中启动主函数,以便它可以监视源文件并在线程更改时重新启动它。使用 use_reloader = False 选项禁用此功能。



如果您希望能够在启动服务器从一个不同的模块,包装在一个函数,并从另一个模块调用该函数:
$ b $ pre $ def $ run_server(dom) :
_run_on_start(%s%dom)
app.run(debug = True,use_reloader = False)

if __name__ =='__main__':
如果len(sys.argv)< 2:
raise Exception(必须为应用程序执行提供域名)
else:
DOM = sys.argv [1]
run_server(DOM)

正确的做法取决于您实际尝试完成的工作。内置的服务器用于在将应用程序部署到生产服务器之前在本地测试环境中运行,因此,从不同的模块启动它的问题本身并没有什么意义。


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)

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.

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.

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.

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

解决方案

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.

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

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