python - Tornado(或者Flask)如何配置多人开发的settings文件?
ringa_lee
ringa_lee 2017-04-18 10:01:17
0
2
689

要实现的效果类似运行应用时:

python app.py --settings=zhangsan
python app.py --settings=lisi

不同的人加载不同的数据库配置,缓存配置等等。

ringa_lee
ringa_lee

ringa_lee

reply all(2)
刘奇

Self-question and answer: Use tornado’s options and define interfaces to read a set of configuration files. The approximate code is as follows:

tornado entry file main.py

define('port', default=9000, help='run on the given port', type=int)
define('debug', default=True, help='debug mode', type=bool)
define('settings', default=None, help='tornado settings file', type=str)
define('config', default=None, help='tornado config file', type=dict)
options.parse_command_line()
if options.settings:
    options.parse_config_file('settings/%s/app_config.py'%(options.settings))
else:
    raise Exception("You must add a xxx.py at settings/ folder, then run: 'python app.py --settings=user'")
The code in

app_config.py is as follows:

from tornado.options import options
import importlib
db_config = importlib.import_module('settings.%s.db_config'%options.settings)

options.config = {
    'MONGO': db_config.MONGO,
    'SETTINGS': {},
}

When running the code

python main.py --settings=xxx
阿神

Leave a standard set of database and cache configuration files unchanged.

Then everyone has their own corresponding database and cache configuration files, and ignore these files (I assume you are using git for version management).

This will not delay everyone’s development, nor will it affect future product releases.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template