>  기사  >  백엔드 개발  >  Python Django 소스 코드의 실행 프로세스 분석

Python Django 소스 코드의 실행 프로세스 분석

WBOY
WBOY앞으로
2023-05-07 12:22:071294검색

Django 실행 시퀀스

  • WSGI는 클라이언트가 보낸 요청을 지속적으로 모니터링합니다

  • 먼저 middleware를 거쳐 분석 및 검증 처리

  • 를 거친 다음 url 배포 및 검증

    을 거칩니다.
  • 处성층 처리

  • 를 수행한 후
  • 중간부분

    분석 및 검증

  • 반환
  • 응답

    콘텐츠

  • 1. 시작

1. 1개의 명령줄 시작(테스트 서버)

명령줄

명령줄

명령줄 결론

: 두 번째 단계에서

utility.execute() 함수

는 명령줄 매개변수에 따라 처리하기 위해 여러 클래스에 배포됩니다관리에 키 코드를 입력하세요 .py Execute_from_command_line(sys.argv)

def main():
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'testDjango.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
		---
    execute_from_command_line(sys.argv)
2 .execute_from_command_line 함수에서 ManagementUtility 클래스를 인스턴스화한 다음
utility.execute() 함수를 실행합니다. [2.1 이 함수는 python manage.py runserverpython manage.py help
2.2 其会通过分析额外添加的参数选择要使用的类或者函数类或者函数对应着djangocoremanagementcommands里面的类

def execute_from_command_line(argv=None):
    utility = ManagementUtility(argv)
    utility.execute()

3.从self.fetch_command(subcommand).run_from_argv(self.argv)[约第413行]
3.1 self.fetch_command(subcommand),这个函数返回了runserver.Command对象(可以自行深入查看),之后执行该Command父类里面的run_from_argv函数

 def execute(self):
			---
        if subcommand == 'help':
			---
        elif subcommand == 'version' or self.argv[1:] == ['--version']:
            sys.stdout.write(django.get_version() + '\n')
        elif self.argv[1:] in (['--help'], ['-h']):
            sys.stdout.write(self.main_help_text() + '\n')
        else:
            self.fetch_command(subcommand).run_from_argv(self.argv)

4.从run_from_argv函数self.execute(*args, **cmd_options)进入
4.1 当前类也有这个execute函数,但是由于继承关系(此时的self也指向Command类),子类如果已经存在该函数会覆盖执行,execute是在子类 Command类中(之后由于super还会到父类里面)[约第354行]

    def run_from_argv(self, argv):
        self._called_from_command_line = True
        parser = self.create_parser(argv[0], argv[1])
        options = parser.parse_args(argv[2:])
        cmd_options = vars(options)

        args = cmd_options.pop('args', ())
        handle_default_options(options)
        try:
            self.execute(*args, **cmd_options)
        except CommandError as e:
			---

5.execute函数执行output = self.handle(*args, **options)[约第398行]跳进子类runserver.Command类的handle函数
5.1 此时位于Command类的父类里面的execute,因为super().execute(*args, **options) #继承下来父类

    def handle(self, *args, **options):
        if not settings.DEBUG and not settings.ALLOWED_HOSTS:
            raise CommandError('You must set settings.ALLOWED_HOSTS if DEBUG is False.')
        self.use_ipv6 = options['use_ipv6']
        if self.use_ipv6 and not socket.has_ipv6:
            raise CommandError('Your Python does not support IPv6.')
        self._raw_ipv6 = False
        if not options['addrport']:
        	---
        else:
           	---
        if not self.addr:
            self.addr = self.default_addr_ipv6 if self.use_ipv6 else self.default_addr
            self._raw_ipv6 = self.use_ipv6
        self.run(**options)

6.handle 函数最后一行,从 self.run(**options) 进入

    def run(self, **options):
        use_reloader = options['use_reloader']

        if use_reloader:
            autoreload.run_with_reloader(self.inner_run, **options)
        else:
            self.inner_run(None, **options)

7.从def inner_run(self, *args, **options)再执行run函数

    def inner_run(self, *args, **options):
    		---
        try:
            handler = self.get_handler(*args, **options)
            run(self.addr, int(self.port), handler,
                ipv6=self.use_ipv6, threading=threading, server_cls=self.server_cls)
        except OSError as e:
          	 ---

8.最后启动服务,此时跳到django.core.servers.basehttp.py的run函数
8.1 httpd_cls = type('WSGIServer', (socketserver.ThreadingMixIn, server_cls), {}) 这一步特别重要,其涉及到较长的继承关系,2.监听-4.1这一环节会介绍到

def run(addr, port, wsgi_handler, ipv6=False, threading=False, server_cls=WSGIServer):
    server_address = (addr, port)
    if threading:
        httpd_cls = type('WSGIServer', (socketserver.ThreadingMixIn, server_cls), {})
    else:
        httpd_cls = server_cls
    httpd = httpd_cls(server_address, WSGIRequestHandler, ipv6=ipv6)
    if threading:
        httpd.daemon_threads = True
    httpd.set_app(wsgi_handler)
    httpd.serve_forever()

总结流程:

  • 解析运行 python manage.py 所提供的参数,例如: help

  • 加载所有的app

  • 根据参数找到相对应的命令管理工具

  • 检查端口ipv4检测ipv6检测端口是否占用线程检查

  • orm对象检查表是否创建

  • 最后启动python Lib库中的WSGIServer

2.监听

解释:WSGI开启后,不间断的监听外界的请求

快速阅读:下面写的比较麻烦,最快了解监听和到中间件前的经过就是去读 1 、12.1 和 13

2.1 runserver(测试服务器)

1.runserver成功开启后,关键的一步是httpd.serve_forever(),其使得进入监听即一个死循环

def run(addr, port, wsgi_handler, ipv6=False, threading=False, server_cls=WSGIServer):
	---
    httpd.set_app(wsgi_handler)
    httpd.serve_forever()

2.在serve_forever()函数里面执行,当ready有值时,表示有请求发来,然后进入self._handle_request_noblock()

    def serve_forever(self, poll_interval=0.5):
        self.__is_shut_down.clear()
        try:
            with _ServerSelector() as selector:
                selector.register(self, selectors.EVENT_READ)
                while not self.__shutdown_request:
                    ready = selector.select(poll_interval)
                    if self.__shutdown_request:
                        break
                    if ready:
                        self._handle_request_noblock()

                    self.service_actions()
		---

3.从self._handle_request_noblock()正常请求将进入self.process_request(request, client_address)특별히 매개 변수를 분석하는 데 사용됩니다

, pythonmanage.py runserver, pythonmanage.py help🎜2.2 🎜추가 매개변수🎜를 분석하여 사용할 🎜클래스 또는 함수🎜를 선택합니다. 🎜 클래스 또는 함수 🎜는 🎜djangocoremanagementcommands🎜 🎜
    def _handle_request_noblock(self):
        try:
            request, client_address = self.get_request()
        except OSError:
            return
        if self.verify_request(request, client_address):
            try:
                self.process_request(request, client_address)
            except Exception:
                self.handle_error(request, client_address)
                self.shutdown_request(request)
            except:
                self.shutdown_request(request)
                raise
        else:
            self.shutdown_request(request)
🎜3의 클래스에 해당합니다. From 🎜self.fetch_command(subcommand).run_from_argv(self.argv)🎜[라인 413]🎜3.1 🎜self.fetch_command(하위 명령) 🎜 , 이 함수는 🎜runserver.Command🎜 개체를 반환하고(직접 세부적으로 확인할 수 있음) 🎜Command🎜 상위 클래스에서 🎜run_from_argv🎜 함수 🎜
    def process_request(self, request, client_address):
        """Start a new thread to process the request."""
        t = threading.Thread(target = self.process_request_thread,
                             args = (request, client_address))
        t.daemon = self.daemon_threads
        if not t.daemon and self.block_on_close:
            if self._threads is None:
                self._threads = []
            self._threads.append(t)
        t.start()
🎜를 실행합니다. 4. 🎜run_from_argv🎜 함수에서 🎜4.1을 입력합니다. 🎜self.execute🎜(*args, **cmd_options). 현재 클래스에도 이 🎜execute🎜 함수가 있지만 상속 관계로 인해(현재 self도 🎜Command 클래스🎜를 가리킴) 하위 클래스가 이미 🎜execute🎜는 하위 클래스 🎜Command 클래스🎜에 있습니다. (나중에 super🎜로 인해 상위 클래스로 이동합니다🎜) [약 354번째 줄]🎜
    def process_request_thread(self, request, client_address):
        """Same as in BaseServer but as a thread.

        In addition, exception handling is done here.

        """
        try:
            self.finish_request(request, client_address)
        except Exception:
            self.handle_error(request, client_address)
        finally:
            self.shutdown_request(request)
🎜🎜5.execute🎜Function 실행🎜 출력 = self.handle(*args, **options)🎜[약 398행] 하위 클래스로 점프🎜runserver .Command🎜 클래스🎜🎜5.1의 🎜handle 함수는 이제 상위 클래스의 🎜execute🎜에 있습니다. 🎜Command 클래스🎜, super().execute(*args, **options) #상위 클래스에서 상속되었기 때문입니다.🎜<pre class="brush:py;">class BaseServer: timeout = None def __init__(self, server_address, RequestHandlerClass): &quot;&quot;&quot;Constructor. May be extended, do not override.&quot;&quot;&quot; self.server_address = server_address self.RequestHandlerClass = RequestHandlerClass self.__is_shut_down = threading.Event() self.__shutdown_request = False</pre>🎜🎜6.handle🎜 함수의 마지막 줄, from 🎜self.run(**options)🎜 🎜<pre class="brush:py;"> def finish_request(self, request, client_address): self.RequestHandlerClass(request, client_address, self) # self.RequestHandlerClass等同于self.WSGIRequestHandler</pre>🎜7.from <code>def inner_run(self, *args, **options) code>를 입력하고 🎜run 함수🎜🎜<pre class="brush:py;">class BaseRequestHandler: def __init__(self, request, client_address, server): self.request = request self.client_address = client_address self.server = server self.setup() try: self.handle() finally: self.finish()</pre>🎜를 실행합니다.8. 서비스를 실행하고 🎜django.core.servers.basehttp.py🎜8.1 <code>httpd_cls = type('WSGIServer', (socketserver .ThreadingMixIn, server_cls), {})의 실행 기능으로 점프합니다. 이 단계는 특히 🎜2. Monitoring-4.1🎜이 링크는 🎜
    def handle(self):
        self.close_connection = True
        self.handle_one_request()
        while not self.close_connection:
            self.handle_one_request()
        try:
            self.connection.shutdown(socket.SHUT_WR)
        except (AttributeError, OSError):
            pass
🎜요약 프로세스에 소개됩니다. 🎜🎜🎜🎜 Python Manage.py를 실행하여 제공되는 매개변수를 구문 분석합니다. 예: help🎜🎜 🎜🎜🎜모든 앱 로드🎜🎜🎜🎜🎜매개변수를 기반으로 해당 🎜명령 관리 도구 찾기🎜🎜🎜🎜🎜🎜포트 확인 🎜, 🎜ipv4 감지 🎜, 🎜ipv6 감지 🎜, 포트가 점유되어 있는지 여부 🎜 , 🎜 스레드 검사 🎜🎜🎜🎜🎜🎜orm🎜 객체 검사 테이블 생성 여부 🎜🎜🎜🎜마지막으로 Python Lib 라이브러리에서 🎜WSGIServer🎜🎜🎜🎜 시작 🎜2. 켜졌다, 외부 요청을 지속적으로 모니터링합니다🎜🎜빠른 읽기: 다음은 작성하기가 더 번거롭습니다.🎜청취와 미들웨어에 도달하기 전🎜프로세스를 이해하는 가장 빠른 방법은🎜1, 12.1 및 13🎜 🎜🎜2.1 runserver(테스트)를 읽는 것입니다. server)🎜🎜1. runserver가 성공적으로 열린 후 핵심 단계는 httpd.serve_forever()입니다. 이는 🎜무한 루프🎜🎜
    def handle_one_request(self):
        """Copy of WSGIRequestHandler.handle() but with different ServerHandler"""
        self.raw_requestline = self.rfile.readline(65537)
        if len(self.raw_requestline) > 65536:
            self.requestline = &#39;&#39;
            self.request_version = &#39;&#39;
            self.command = &#39;&#39;
            self.send_error(414)
            return

        if not self.parse_request():  # An error code has been sent, just exit
            return

        handler = ServerHandler(
            self.rfile, self.wfile, self.get_stderr(), self.get_environ()
        )
        handler.request_handler = self      # backpointer for logging & connection closing
        handler.run(self.server.get_app())
🎜2인 🎜listening🎜에 진입하게 합니다. serve_forever() 함수에서 실행됩니다. 🎜ready🎜에 값이 있으면 요청이 전송되었음을 의미하며, 이후 self._handle_request_noblock()🎜
    def run(self, application):
        try:
            self.setup_environ()
            self.result = application(self.environ, self.start_response)
            self.finish_response()
        except (ConnectionAbortedError, BrokenPipeError, ConnectionResetError):

            return
        except:
			---
🎜을 입력합니다. 3. self._handle_request_noblock()에서 일반 요청은 self.process_request(request, client_address)를 입력합니다🎜
    def _handle_request_noblock(self):
        try:
            request, client_address = self.get_request()
        except OSError:
            return
        if self.verify_request(request, client_address):
            try:
                self.process_request(request, client_address)
            except Exception:
                self.handle_error(request, client_address)
                self.shutdown_request(request)
            except:
                self.shutdown_request(request)
                raise
        else:
            self.shutdown_request(request)

4.从self.process_request(request, client_address)进入来到了ThreadingMixIn.process_request
4.1 此时,如果没有搞清楚此时的self是谁,就搞不明白为什么进入到ThreadingMixIn.process_request,而不是其它的process_request,这时候就关联到上面提到的httpd_cls = type('WSGIServer', (socketserver.ThreadingMixIn, server_cls), {})
4.2 type的用法是动态的创建类,此时httpd_cls 是一个新类,里面分别继承了ThreadingMixIn和server_cls对应得WSGIServer,这时就不难理解为什么找的是ThreadingMixIn.process_request

    def process_request(self, request, client_address):
        """Start a new thread to process the request."""
        t = threading.Thread(target = self.process_request_thread,
                             args = (request, client_address))
        t.daemon = self.daemon_threads
        if not t.daemon and self.block_on_close:
            if self._threads is None:
                self._threads = []
            self._threads.append(t)
        t.start()

5.在def process_request(self, request, client_address)里面的t = threading.Thread(target = self.process_request_thread,args = (request, client_address))实际调用了self.process_request_thread,但是等t.start()才会真正执行

    def process_request_thread(self, request, client_address):
        """Same as in BaseServer but as a thread.

        In addition, exception handling is done here.

        """
        try:
            self.finish_request(request, client_address)
        except Exception:
            self.handle_error(request, client_address)
        finally:
            self.shutdown_request(request)

6.从def process_request_thread(self, request, client_address)进入,self.finish_request(request, client_address),继续完成请求
6.1 这时候又需要回顾之前的代码,因为self.RequestHandlerClass不是已经有的类,而是初始化的时候赋值,其值变为了某个类
6.2 这个过程就在1.启动-8里面的httpd = httpd_cls(server_address, WSGIRequestHandler, ipv6=ipv6),此时的httpd_cls是type动态创建的,继承了ThreadingMixIn和server_cls对应得WSGIServer,实例化时会执行def __init__方法,其关键执行了self.RequestHandlerClass = RequestHandlerClass

class BaseServer:
    timeout = None

    def __init__(self, server_address, RequestHandlerClass):
        """Constructor.  May be extended, do not override."""
        self.server_address = server_address
        self.RequestHandlerClass = RequestHandlerClass
        self.__is_shut_down = threading.Event()
        self.__shutdown_request = False
    def finish_request(self, request, client_address):
        self.RequestHandlerClass(request, client_address, self)
        # self.RequestHandlerClass等同于self.WSGIRequestHandler

7.从self.RequestHandlerClass(request, client_address, self),即去WSGIRequestHandler类里面初始化,根据一层层继承关系,只要最老类BaseRequestHandler有初始化方法

class BaseRequestHandler:
    def __init__(self, request, client_address, server):
        self.request = request
        self.client_address = client_address
        self.server = server
        self.setup()
        try:
            self.handle()
        finally:
            self.finish()

def __init__(self, request, client_address, server):进入self.handle()
8.1 此时的self.handle(),根据继承关系,其就在最小子类WSGIRequestHandler里面

    def handle(self):
        self.close_connection = True
        self.handle_one_request()
        while not self.close_connection:
            self.handle_one_request()
        try:
            self.connection.shutdown(socket.SHUT_WR)
        except (AttributeError, OSError):
            pass

9.从def handle(self)进入self.handle_one_request()

    def handle_one_request(self):
        """Copy of WSGIRequestHandler.handle() but with different ServerHandler"""
        self.raw_requestline = self.rfile.readline(65537)
        if len(self.raw_requestline) > 65536:
            self.requestline = &#39;&#39;
            self.request_version = &#39;&#39;
            self.command = &#39;&#39;
            self.send_error(414)
            return

        if not self.parse_request():  # An error code has been sent, just exit
            return

        handler = ServerHandler(
            self.rfile, self.wfile, self.get_stderr(), self.get_environ()
        )
        handler.request_handler = self      # backpointer for logging & connection closing
        handler.run(self.server.get_app())

10.从def handle_one_request(self)进入handler.run(self.server.get_app())
10.1 注意此时handler为ServerHandler实例化对象,run方法存在它的最大父类BaseHandler里面
10.2 此时handler.run(self.server.get_app())执行了self.server.get_app(),其返回django.contrib.staticfiles.handlers.StaticFilesHandlerhandler.run把其当参数传递了过去

    def run(self, application):
        try:
            self.setup_environ()
            self.result = application(self.environ, self.start_response)
            self.finish_response()
        except (ConnectionAbortedError, BrokenPipeError, ConnectionResetError):

            return
        except:
			---

11.从def run(self, application)进入self.result = application(self.environ, self.start_response),其中applicationdjango.contrib.staticfiles.handlers.StaticFilesHandler
11.1 其中self.application已经初始化了是WSGIHandler

class StaticFilesHandler(StaticFilesHandlerMixin, WSGIHandler):
    def __init__(self, application):
        self.application = application
        self.base_url = urlparse(self.get_base_url())
        super().__init__()

    def __call__(self, environ, start_response):
        if not self._should_handle(get_path_info(environ)):
            return self.application(environ, start_response)
        return super().__call__(environ, start_response)

12.进入后执行def __call__(self, environ, start_response)方法,进入return self.application(environ, start_response),此时self.application已经初始化了是WSGIHandler
12.1 request = self.request_class(environ)获取到用户请求的url后面就开始配置runserver启动时候加载的url; response = self.get_response(request)获取用户url对应的响应准备开始往视图转

    def __call__(self, environ, start_response):
        set_script_prefix(get_script_name(environ))
        signals.request_started.send(sender=self.__class__, environ=environ)
        request = self.request_class(environ)
        response = self.get_response(request)
		---

13.进入response = self.get_response(request),结束,再下一步就要开始中间件的进行

    def get_response(self, request):
        set_urlconf(settings.ROOT_URLCONF)
        response = self._middleware_chain(request)
        response._resource_closers.append(request.close)
        if response.status_code >= 400:
            log_response(
                &#39;%s: %s&#39;, response.reason_phrase, request.path,
                response=response,
                request=request,
            )
        return response

3.中间件的执行

解释:中间件的执行需要联系着上面运行过程,这个过程是一个递归的过程,下面介绍的五个函数中间件命名规则对应得内容

  • process_request():完成请求对象的创建,但用户访问的网址尚未与网站的路由地址匹配。

  • process_view():完成用户访问的网址与路由地址的匹配,但尚未执行视图函数。

  • process_exception():在执行视图函数的期间发生异常,比如代码异常,主动抛出404异常等。

  • process_response():完成视图函数的执行,但尚未将响应内容返回浏览器

  • process_template_response():默认不执行,在视图函数完成操作后调用,除非视图函数返回的response中有render方法(几乎不会用,可以忽略)

1.递归的进入阶段:循环进行下面的代码(此代码位置django\core\handlers\exception.py

1.1 此处出现process_request()process_response()

        @wraps(get_response)
        def inner(request):
            try:
                response = get_response(request) # 此进入循环
            except Exception as exc:
                response = response_for_exception(request, exc)
            return response
        return inner
    def __call__(self, request):
        # Exit out to async mode, if needed
        if asyncio.iscoroutinefunction(self.get_response):
            return self.__acall__(request)
        response = None
        if hasattr(self, &#39;process_request&#39;):
            response = self.process_request(request) # 进行中间件的process_request步骤
        response = response or self.get_response(request) # 此进入循环
        if hasattr(self, &#39;process_response&#39;):
            response = self.process_response(request, response) # 此是递归后执行的
        return response

2.递归的结束准备回传:进行下面的代码(此代码位置django\core\handlers\base.py
2.1 此处出现process_view()process_template_response()process_exception()

进入视图的关键函数:

  • callback, callback_args, callback_kwargs = self.resolve_request(request) # callback即对于视图函数url匹配到对应的view函数

  • for middleware_method in self._view_middleware_view_middleware里面放着所有的process_view()函数(初始化时加载的), process_view()正是在该代码下面的环节循环执行

  • response = wrapped_callback(request, *callback_args, **callback_kwargs) 回调函数传参,并返回试图函数响应。

  • 沿着这个路径连续进入两次,就到了后面讲到的as_view里面(此内容是专门视图处理的前的关键步骤)

  • response = self.process_exception_by_middleware(e, request)对应process_exception()

  • self._template_response_middleware 循环加载模板中间件

 def _get_response(self, request):
        response = None
        callback, callback_args, callback_kwargs = self.resolve_request(request)
        for middleware_method in self._view_middleware:
            response = middleware_method(request, callback, callback_args, callback_kwargs)
            if response:
                break
        if response is None:
            wrapped_callback = self.make_view_atomic(callback) # 找到视图函数
            # If it is an asynchronous view, run it in a subthread.
            if asyncio.iscoroutinefunction(wrapped_callback):
                wrapped_callback = async_to_sync(wrapped_callback)
            try:
                response = wrapped_callback(request, *callback_args, **callback_kwargs)
            except Exception as e:
                response = self.process_exception_by_middleware(e, request)
                if response is None:
                    raise
        self.check_response(response, callback)
        if hasattr(response, &#39;render&#39;) and callable(response.render):
            for middleware_method in self._template_response_middleware:
                response = middleware_method(request, response)
                self.check_response(
                    response,
                    middleware_method,
                    name=&#39;%s.process_template_response&#39; % (
                        middleware_method.__self__.__class__.__name__,
                    )
                )
            try:
                response = response.render()
            except Exception as e:
                response = self.process_exception_by_middleware(e, request)
                if response is None:
                    raise
        return response

3.递归的结束回传:循环进行下面的代码

        @wraps(get_response)
        def inner(request):
            try:
                response = get_response(request) # 此进入循环
            except Exception as exc:
                response = response_for_exception(request, exc)
            return response
        return inner
    def __call__(self, request):
        # Exit out to async mode, if needed
        if asyncio.iscoroutinefunction(self.get_response):
            return self.__acall__(request)
        response = None
        if hasattr(self, &#39;process_request&#39;):
            response = self.process_request(request) 
        response = response or self.get_response(request) # 此进入循环
        if hasattr(self, &#39;process_response&#39;):
            response = self.process_response(request, response) # 进行中间件的process_response步骤
        return response

위 내용은 Python Django 소스 코드의 실행 프로세스 분석의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 yisu.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제