问题描述
在我的Flask应用程序的 config.py 中,我有 LocalConfig 和 ProdConfig $
现在我正在使用 uWsgi 将应用程序提供给 Nginx ,这里创建了 myapp.wsgi 。
from myapp import create_app $ b $ from myapp.config import ProdConfig
$ b $ app = create_app(config = ProdConfig)
以及其中一个 app.py 被定义为:
$ $ $ $ c $ def create_app(config = None,app_name = None, blueprints = None):
#一些代码
app = Flask(app_name,instance_path = INSTANCE_FOLDER_PATH,instance_relative_config = True)
configure_app(app,config)
#其他代码
返回应用程序
def configure_app(应用程序,配置=无):
不同的配置方式。
app.config.f rom_object(LocalConfig)
app.config.from_pyfile('production.cfg',silent = True)
如果配置:
app.config.from_object(config)
我想知道,是否正确地使用 uWSGI ? uWSGI能够成功应用 ProdConfig 吗?
或者使用环境变量来区分不同的配置设置?像 if os.environ.get('PROD',True):#do something ?
更好? Flask的 create_app()一个还是env变量?任何其他合适的方法?
一个常见的方法是将配置存储在字典中。
$ b
class Config:
ALL_CAPS_CONFIG ='SOME VALUE'
$ b $ class DevConfig(Config):
pass
$ b $ class TestConfig(Config):
pass
$ b $ class ProdConfig(Config):
pass
configs = {
'dev':DevConfig,
'test':TestConfig,
'prod':ProdConfig,
'default':ProdConfig
}
然后,在实际创建应用程序的位置,您可以执行下列操作:
from config import configs
import os
evn = os.environ.get('MY_FLASK_APP_ENV','default')
create_app(config = configs [evn])
在环境之间轻松地通过更改您的shell中的变量。
In my Flask application's config.py i have LocalConfig and ProdConfig and latter is to be used in production, once app is deployed.
Now i'm using uWsgi to serve app to Nginx and here the myapp.wsgi i have created.
from myapp import create_app from myapp.config import ProdConfig app = create_app(config=ProdConfig)
and in one of other app.py create_app is defined as:
def create_app(config=None, app_name=None, blueprints=None): # some code app = Flask(app_name, instance_path=INSTANCE_FOLDER_PATH, instance_relative_config=True) configure_app(app, config) # some other code return app def configure_app(app, config=None): """Different ways of configurations.""" app.config.from_object(LocalConfig) app.config.from_pyfile('production.cfg', silent=True) if config: app.config.from_object(config)
I want to know, will it properly with uWSGI? Will uWSGI be able to applying the ProdConfig successfully?
Or is it better to use Environment Variables to distinguish between different config settings? Like if os.environ.get('PROD', True): #do something?
Which way is better? Flask's create_app() one or the env variable one? Any other suitable approach?
A common way of doing this is storing the configurations in a dictionary.
class Config: ALL_CAPS_CONFIG = 'SOME VALUE' class DevConfig(Config): pass class TestConfig(Config): pass class ProdConfig(Config): pass configs = { 'dev' : DevConfig, 'test' : TestConfig, 'prod' : ProdConfig, 'default' : ProdConfig }
And then where you actually create your app, you'd do something like this::
from config import configs import os evn = os.environ.get('MY_FLASK_APP_ENV', 'default') create_app(config=configs[evn])
That way you can easily switch between environments easily by changing a variable in your shell.
这篇关于如何使用create_app将flask应用程序中的不同配置设置提供给uwsgi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!