標籤:技術 led 配置 erro ase 匹配 遞迴調用 parallel label
先瞭解下官方文檔的介紹
Django包含了一個已經安裝應用的註冊表,這個註冊表格儲存體著配置資訊以及用來自省,同時也維護著可用模型的列表。
這個註冊表就是apps,位於django.apps下,本質上是一個django.apps.registry模組下的一個Apps類的執行個體。
源碼面前見真相:
class Apps(object):
這是一個註冊表,儲存著已安裝應用的配置資訊,同時也跟蹤著模型,例如提供reverse-relations。
初始化的主要屬性:
apps_configs:這是一個有順序的字典,將AppConfig執行個體的label標籤映射到已經安裝的entry(AppConfig)上。
主要方法:
populate(installded_app):載入應用的配置資訊和模型,這個方法匯入每個應用模組然後是每個模型模組,它是安全執行緒並且是等冪(任意多次執行所產生的影響均與一次執行的影響相同)的,但是不能重入(reentrant),簡單百度下,應該是不能進行遞迴調用的意思。
if self.ready: #該註冊表是否已經被填充 return # populate() might be called by two threads in parallel on servers # that create threads before initializing the WSGI callable.
# populate()在初始化WSGI調用之前,可能會被會建立線程的伺服器上的兩個並行的線程調用。 with self._lock:
#_lock()為Theading.Lock()對象,在with上下文管理器中自動擷取鎖,處理過後,自動釋放鎖。 if self.ready: return # app_config should be pristine, otherwise the code below won‘t # guarantee that the order matches the order in INSTALLED_APPS.
# app_config應該是處於原始的狀態,否則下面的代碼將不能保證這個順序匹配INSTALLED_APPS中的順序。
if self.app_configs: raise RuntimeError("populate() isn‘t reentrant") # Phase 1: initialize app configs and import app modules. for entry in installed_apps:
# 迭代已安裝apps的每一項 if isinstance(entry, AppConfig): app_config = entry else:
# 若INSTALLED_APPS中配置的不是AppConfig類的Python路徑而是App模組路徑,將會以工廠方式進行建立。 app_config = AppConfig.create(entry) if app_config.label in self.app_configs:
# 檢測app_config的唯一性。 raise ImproperlyConfigured( "Application labels aren‘t unique, " "duplicates: %s" % app_config.label) self.app_configs[app_config.label] = app_config # app_config.label屬性預設是由app_name擷取的。 app_config.apps = self #將註冊表賦給AppConfig類的apps屬性。 # Check for duplicate app names. counts = Counter( app_config.name for app_config in self.app_configs.values()) duplicates = [ name for name, count in counts.most_common() if count > 1] if duplicates: raise ImproperlyConfigured( "Application names aren‘t unique, " "duplicates: %s" % ", ".join(duplicates)) self.apps_ready = True # Phase 2: import models modules.
# 匯入模型模組
for app_config in self.app_configs.values(): app_config.import_models() self.clear_cache() self.models_ready = True # Phase 3: run ready() methods of app configs.
# 運行apps configs的ready()方法。
for app_config in self.get_app_configs(): app_config.ready() self.ready = True
Django之Apps源碼學習