I like to start my flask projects with a simple __name__ == "__main__" with arparse and app.run(). But obviously the flask inbuilt server is not the fastest and probably not the safest either. This is why you should eventually use a wsgi runner like waitress to run your app. This post will show how to migrate, while keeping any standalone capabilities.
我喜欢使用带有arparse和app.run()的简单__name__ == "__main__"来启动我的flask项目。 但是很明显,烧瓶内置服务器不是最快的,也可能不是最安全的。 这就是为什么您最终应该使用服务员之类的wsgi赛跑者来运行您的应用程序的原因。 这篇文章将展示如何迁移,同时保持所有独立功能。
In short, everything in your if __name__ block is not going to be executed anymore, the simplest solution is to move it to a @app.before_first_request anotated function, so:
简而言之, if __name__块中的所有内容都将不再执行,最简单的解决方案是将其移至带有@app.before_first_request注释的函数中,因此:
app = Flask("LOL")if __init__ == "__name__": # arparse stuff ... # init stuff ... app.run(host="127.0.0.1", port=5300)becomes:
变成:
app = Flask("LOL")@app.before_first_requestdef doStuff(): # init stuff ...if __init__ == "__name__": # arparse stuff ... # write arparse results to config app.run(host="127.0.0.1", port=5300)Obviously we now need to create an alternative option for configuration to argparse. When the app is started through WSGI and write any argparse-inputs to the runtime version of this config so that it can be used in our init function and we can avoid duplicated code. You can create a config.py and load it with app.config.from_object or you can simply use:
显然,我们现在需要为argparse配置创建替代选项。 当通过WSGI启动应用程序并将所有argparse -inputs写入此配置的运行时版本时,可以在我们的init函数中使用它,并且可以避免代码重复。 您可以创建一个config.py并使用app.config.from_object加载它,也可以简单地使用:
app.config['OPTION'] = "something"We can then change it like a namespace-object (basicly a dict-object). Remember that any options you pass to app.run don’t have to be/can’t be written to app.config, as they will later be supplied by the WSGI-runner configuration.
然后,我们可以像命名空间对象(基本上是字典对象)一样更改它。 请记住,您传递给app.run的任何选项都不必/不能写入app.config ,因为它们稍后将由WSGI-runner配置提供。
config.py:
config.py:
SOME_OPTION=AANOTHER_VARIABLE=42server.py:
server.py:
app = Flask("LOL")app.config.from_object("config") # note the missing '.py'app.config["MORE_OPTIONS"] = "Hello"@app.before_first_requestdef doStuff(): doInitSuff(app.config.SOME_OPTION)if __init__ == "__name__": parser = argparse.ArgumentParser() parser.add_argument(...) args = parse.parse_args() app.config['SOME_OPTION'] = args.SOME_OPTIONS app.run(host="127.0.0.1", port=53000)Finally you need to add an entry point. The simplest way to do this, is to create a new file and add a really simple function, which returns the flask application in your main module, e.g. the following app.py with ‘app’ being the global variable app in server.py, aka your main file, presumably containing all the anontated functions/webserver paths etc. :
最后,您需要添加一个入口点。 最简单的方法是创建一个新文件并添加一个非常简单的函数,该函数将在您的主模块中返回flask应用程序,例如,以下app.py,其中“ app”是server.py中的全局变量app ,又名您的主文件,大概包含所有带注释的功能/网络服务器路径等:
# note the missing'.py'import server as moduleContainingApp# default value is requireddef createApp(envivorment=None, start_response=None): return moduleContainingApp.appYou could also use the enviroment for on the fly configuration.
您也可以将环境用于即时配置。
The simplest production server for the WSGI-protocol is waitress, you can install it with pip, or via apt as python3-waitress on Debian9+. Then run it with:
WSGI协议最简单的生产服务器是waitress ,您可以使用pip安装它,也可以通过apt作为python3-waitress在Debian9 +上安装它。 然后运行:
waitress-serve --host 127.0.0.1 --port 5300 --call 'app:createApp'createApp is your entrypoint, app your app.py containing this entry point.
createApp是您的入口点, app是包含此入口点的app.py。
Write this to a file called NAME.service in /etc/systemd/user/
将此文件写入 / etc / systemd / user /中名为NAME.service的文件中
[Unit]Description=Hello FlaskAfter=network.target[Service]WorkingDirectory=/path/to/appDir/Type=simpleUser=www-dataExecStart=/usr/bin/waitress-serve/ --host 127.0.0.1 --port 5004 --call 'app:createApp'[Install]WantedBy=multi-user.targetEnable it with it’s full path, then start it by it’s name:
使用完整路径启用它,然后使用其名称启动它:
systemctl enable /etc/systemd/user/NAME.servicesystemctl start NAMENow it will run and start automaticly on reboot. No more excuses to use cron-@reboot. You people anoy me. Yes I’m looking at you.
现在它将运行并在重新启动时自动启动。 没有更多的借口使用cron- @ reboot 。 你们讨厌我。 是的,我在看着你。
Below are some of the commits, which implemented this transition:
以下是一些实现了此过渡的提交:
Serienampel
Serienampel
Leaderboard
排行榜
Webhook Handler
Webhook处理程序
Simple Poll
简单投票
Originally published at https://blog.atlantishq.de.
最初发布在 https://blog.atlantishq.de 。
翻译自: https://medium.com/@erlangen_sheppy/flask-standalone-to-wsgi-app-d018b2740020
相关资源:微信小程序源码-合集6.rar