Python Django源碼運行過程解析

本文隻算是本人片面之言(當然也會借鑒網絡上公開資料),而且技術含量比較低,內容質量也一般,大傢僅限參考即可

如果對本文看不太懂,請先閱讀後面文章,等都差不多看完再回顧來看

一、Django運行順序

  • WSGI會不斷監聽客戶端發送來的請求
  • 先經過中間件進行分析驗證處理
  • 然後經過url分發與驗證
  • 視圖層進行處理
  • 再經過中間件進行分析驗證處理
  • 返回響應內容

1.啟動

1.1 命令行啟動(測試服務器)

命令行結論:其在第二步utility.execute()函數會根據命令行參數,分發給不同的類進行處理

在manange.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 其會通過分析額外添加的參數選擇要使用的類或者函數類或者函數對應著django\core\management\commands裡面的類

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)

    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 = ''
            self.request_version = ''
            self.command = ''
            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(
                '%s: %s', 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, 'process_request'):
            response = self.process_request(request) # 進行中間件的process_request步驟
        response = response or self.get_response(request) # 此進入循環
        if hasattr(self, 'process_response'):
            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, 'render') and callable(response.render):
            for middleware_method in self._template_response_middleware:
                response = middleware_method(request, response)
                self.check_response(
                    response,
                    middleware_method,
                    name='%s.process_template_response' % (
                        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, 'process_request'):
            response = self.process_request(request) 
        response = response or self.get_response(request) # 此進入循環
        if hasattr(self, 'process_response'):
            response = self.process_response(request, response) # 進行中間件的process_response步驟
        return response

到此這篇關於Python Django源碼運行過程的文章就介紹到這瞭,更多相關Python Django源碼運行內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: