django加载及启动WSGI服务

  • django加载及启动WSGI服务
    • 入口
    • 加载settings
    • 加载app和app中的models
    • 运行
      • 题外话
      • 正文

django加载及启动WSGI服务

版本 – 1.8.18

VERSION = (1, 8, 18, 'final', 0)

入口

from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

utility.execute()

加载settings

try:
    settings.INSTALLED_APPS #这里实际上真正想执行的是LazySettings中的_setup
except ImproperlyConfigured as exc:
    # 如果django没有设置
    # ENVIRONMENT_VARIABLE = "DJANGO_SETTINGS_MODULE"
    # 会触发异常跳到这里
    self.settings_exception = exc
    # A handful of built-in management commands work without settings.
    # Load the default settings -- where INSTALLED_APPS is empty.
    if subcommand in no_settings_commands:
        settings.configure() # configure也处理了settings.py这里应该是django留了一个钩子给用户定制的

self._wrapped = Settings(settings_module)

self.wrapped就成了settings.py的包装。之后查询settings中的配置,只能从self.wrapped去查询了。

到此settings加载完毕 – 不过我没想明白为什么要费这么大劲去解析settings.py

加载app和app中的models

接着往下运行.

if settings.configured:
    # Start the auto-reloading dev server even if the code is broken.
    # The hardcoded condition is a code smell but we can't rely on a
    # flag on the command class because we haven't located it yet.
    if subcommand == 'runserver' and '--noreload' not in self.argv:
        try:
            autoreload.check_errors(django.setup)()
        except Exception:
            # The exception will be raised later in the child process
            # started by the autoreloader. Pretend it didn't happen by
            # loading an empty list of applications.
            apps.all_models = defaultdict(OrderedDict)
            apps.app_configs = OrderedDict()
            apps.apps_ready = apps.models_ready = apps.ready = True

    # In all other cases, django.setup() is required to succeed.
    else:
        django.setup()

这里的check_errors是一个装饰器(或者说闭包也行),实际运行的仍然是django.setup().

app.populate()很重要

# django/__init__.py
def setup():
    """
    Configure the settings (this happens as a side effect of accessing the
    first setting), configure logging and populate the app registry.
    """
    from django.apps import apps
    from django.conf import settings
    from django.utils.log import configure_logging

    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
    apps.populate(settings.INSTALLED_APPS)

create方法中

  • module = import_module(entry) 导入module

这里导入model

# django/apps/config.py
@classmethod
    def create(cls, entry):
        """
        Factory that creates an app config from an entry in INSTALLED_APPS.
        """
        try:
            # If import_module succeeds, entry is a path to an app module,
            # which may specify an app config class with default_app_config.
            # Otherwise, entry is a path to an app config class or an error.
            module = import_module(entry)

app和app中的models导入完毕.

运行

真正干活的地方.

题外话

无意间发现django command都在这里 :)

django会遍历每个应用下面的management/commands.
django加载及启动WSGI服务_第1张图片
django加载及启动WSGI服务_第2张图片
这里写图片描述

正文

django的command模块代码量有点多…,来不及拆解.

结论在这儿

django加载及启动WSGI服务_第3张图片

你可能感兴趣的:(#,Django,backend)