???????????????????
????????? ???????[ 2017/2/7 11:04:38 ] ?????????????????? ???ù???
???????
????????????????????????????????????????????????????????????????????????????????????μ???????????????????????в??????????????????????????н?????????????????????????????????????????·????????????????????????????????????·???????????????????
????????
????????????? web ???????????????????????????????????????????????????????????? supervisor ?????в??????? supervisor ??????к??????? ???? ????????????????????????????????????????????????????????? “??????????????” ???????????????????????????????????
???????1??
??????????? ip ????????????????λ???????????????? ip ?б????ж???????????????У???????????
???????2??
???????????????????????????????????????????????????????????????????????????????????????????????????????????
???????????
??????????????????????????????????supervisor ?????????????????????????????????????????????????????????
[program:your_project]
command=your_project/env/bin/python your_project/app.py
directory=your_project
environment=PATH="/root/bin:%(ENV_PATH)s"??PYTHONPATH="your_project:$(ENV_PYTHONPATH)s"??PROJECT_ENV="dev"
exitcodes=0
redirect_stderr = true
autorestart = true
stdout_logfile = /data/log/project.log
stdout_logfile_maxbytes = 50MB
stdout_logfile_backups = 2
stderr_logfile = /data/log/project.log
loglevel = info
stopasgroup = True
?????????????????
????command=your_project/env/bin/python your_project/app.
???????????????????????????????????????????μ??????????????????????????????????
??????????
????environment=PATH="/root/bin:%(ENV_PATH)s"??PYTHONPATH="your_project:$(ENV_PYTHONPATH)s"??PROJECT_ENV="dev"
?????????????????????????????????????????????????????????????????????????????????????????
??????????????????????????????????????ж??????????????????????????????????д?? .py ????е??????????????????????????? yaml ??????????????????£??????????????????? settings/ ?????? __init__.py?? dev.py ??? prod.py?????′?????д?? settings/__init__.py ????????д????????????α?????????????ü????????
import importlib
import os
import logging
from settings.dev import *
# ???????????????
env = os.getenv('PROJECT_ENV')
# ??????е???????
global_variable = globals()
prod = dict()
if env == 'prod':
# ??? importlib ?????? .py ??????????
# ???????????????????????????????
# ????? settings.prod ??????????????·??
prod = importlib.import_module('settings.prod')
for key in dir(prod):
if not key.startswith('__'):
# ?????????е??????????????? prod ??????????壬???????????
global_variable[key] = getattr(prod?? key)
????????????????????????????????????Щд sdk ???????????????????????????????????????????????????????д?? sdk??????д??????????????????????£?
class SettingManager(object):
def __init__(self?? env_var?? setting_dir):
"""
"""
self._content = dict()
self.env_var = env_var
self.env = os.getenv(self.env_var?? None)
if self.env:
self.read('.'.join([setting_dir?? self.env]))
def __del__(self):
self._content.clear()
def __getitem__(self?? item):
return self._content.get(item?? None)
def __setitem__(self?? key?? value):
self._content[key] = value
return value
def __getattr__(self?? item):
return self._content.get(item?? None)
def __setattr__(self?? key?? value):
if key.startswith('_'):
object.__setattr__(self?? key?? value)
self._content[key] = value
return value
def read(self?? relative_path):
"""read settings from python file"""
try:
settings = importlib.import_module(relative_path)
except ImportError:
raise ImportError('Wrong relative path provided.')
keys = [key for key in dir(settings) if not key.startswith('__')]
for key in keys:
# same key in different files may cause cover problem
self._content[key] = getattr(settings?? key)
return self
????SettingManager ???????????????????????????????????滹???? magic method?????????????????????????????????????????????????????
>>> from settings import SettingManager
>>> setting = SettingManger('PROJECT_MANAGER'?? 'settings')
>>> setting.PROJECT_CONFIG1
"for zhihu"
>>> setting['PROJECT_CONFIG1'] = 1
>>> setting['PROJECT_CONFIG1']
1
??????????????????????????? sdk ?????????????????????? SettingManager ????????????????趨???????????????????·?????????????SettingManger('PROJECT_MANAGER'?? 'settings')???????????????? dev ???? prod ?????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11