自问自答:使用tornado的options和define接口读取一套配置文件,大致代码如下:
tornado入口文件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'")
app_config.py 中的代码如下:
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': {},
}
运行代码时
python main.py --settings=xxx