Django 響應數(shù)據(jù)response的返回源碼詳解
響應數(shù)據(jù)的返回
在 WSGIHandler.__call__(self, environ, start_response) 方法調(diào)用了 WSGIHandler.get_response() 方法, 由此得到響應數(shù)據(jù)對象 response. 如今所要做的, 便是將其返回給客戶端. 在 Django 源碼小剖: 初探 WSGI 中, 簡要的概括了請求到來時 django 自帶服務器的執(zhí)行關系, 摘抄如下:
- make_server() 中 WSGIServer 類已經(jīng)作為服務器類, 負責接收請求, 調(diào)用 application 的處理, 返回相應;
- WSGIRequestHandler 作為請求處理類, 并已經(jīng)配置在 WSGIServer 中;
- 接著還設置了 WSGIServer.application 屬性(set_app(app));
- 返回 server 實例.
- 接著打開瀏覽器, 即發(fā)起請求. 服務器實例 WSGIServer httpd 調(diào)用自身 handle_request() 函數(shù)處理請求. handle_request() 的工作流程如下:請求-->WSGIServer 收到-->調(diào)用 WSGIServer.handle_request()-->調(diào)用 _handle_request_noblock()-->調(diào)用 process_request()-->調(diào)用 finish_request()-->finish_request() 中實例化 WSGIRequestHandler-->實例化過程中會調(diào)用 handle()-->handle() 中實例化 ServerHandler-->調(diào)用 ServerHandler.run()-->run() 調(diào)用 application() 這才是真正的邏輯.-->run() 中在調(diào)用 ServerHandler.finish_response() 返回數(shù)據(jù)-->回到 process_request() 中調(diào)用 WSGIServer.shutdown_request() 關閉請求(其實什么也沒做)
事實上, WSGIServer 并沒有負責將響應數(shù)據(jù)返回給客戶端, 它將客戶端的信息(如最重要的客戶端 socket 套接字)交接給了 WSGIRequestHandler, WSGIRequestHandler 又將客戶端的信息交接給了 ServerHandler, 所以 ServerHandler 產(chǎn)生響應數(shù)據(jù)對象后, 會直接返回給客戶端.
代碼剖析
從「調(diào)用 ServerHandler.run()-->run() 調(diào)用 application() 這才是真正的邏輯.-->run() 中在調(diào)用 ServerHandler.finish_response() 返回數(shù)據(jù)」開始說起, 下面是主要的代碼解說:
# 下面的函數(shù)都在 ServerHandler 的繼承鏈上方法, 有些方法父類只定義了空方法, 具體邏輯交由子類實現(xiàn). 有關繼承鏈請參看: http://daoluan.net/blog/decode-django-wsgi/ def run(self, application): """Invoke the application""" try: self.setup_environ() # application 在 django 中就是 WSGIHandler 類, 他實現(xiàn)了 __call__ 方法, 所以行為和函數(shù)一樣. self.result = application(self.environ, self.start_response) self.finish_response() except: # handle error def finish_response(self): try: if not self.result_is_file() or not self.sendfile(): for data in self.result: # 向套接字寫數(shù)據(jù), 將數(shù)據(jù)返回給客戶端 self.write(data) self.finish_content() finally: self.close() def write(self, data): """'write()' callable as specified by PEP 333""" # 必須是都是字符 assert type(data) is StringType,"write() argument must be string" if not self.status: raise AssertionError("write() before start_response()") # 需要先發(fā)送 HTTP 頭 elif not self.headers_sent: # Before the first output, send the stored headers self.bytes_sent = len(data) # make sure we know content-length self.send_headers() # 再發(fā)送實體 else: self.bytes_sent += len(data) # XXX check Content-Length and truncate if too many bytes written? self._write(data) self._flush() def write(self, data): """'write()' callable as specified by PEP 3333""" assert isinstance(data, bytes), "write() argument must be bytestring" # 必須先調(diào)用 self.start_response() 設置狀態(tài)碼 if not self.status: raise AssertionError("write() before start_response()") # 需要先發(fā)送 HTTP 頭 elif not self.headers_sent: # Before the first output, send the stored headers self.bytes_sent = len(data) # make sure we know content-length self.send_headers() # 再發(fā)送實體 else: self.bytes_sent += len(data) # XXX check Content-Length and truncate if too many bytes written? 是否需要分段發(fā)送過大的數(shù)據(jù)? # If data is too large, socket will choke, 窒息死掉 so write chunks no larger # than 32MB at a time. # 分片發(fā)送 length = len(data) if length > 33554432: offset = 0 while offset < length: chunk_size = min(33554432, length) self._write(data[offset:offset+chunk_size]) self._flush() offset += chunk_size else: self._write(data) self._flush() def _write(self,data): # 如果是第一次調(diào)用, 則調(diào)用 stdout.write(), 理解為一個套接字對象 self.stdout.write(data) # 第二次調(diào)用就是直接調(diào)用 stdout.write() 了 self._write = self.stdout.write
接下來的事情, 就是回到 WSGIServer 關閉套接字, 清理現(xiàn)場, web 應用程序由此結(jié)束; 但服務器依舊在監(jiān)聽(WSGIServer 用 select 實現(xiàn))是否有新的請求, 不展開了.
階段性的總結(jié)
請求到來至數(shù)據(jù)相應的流程已經(jīng)走了一遍, 包括 django 內(nèi)部服務器是如何運作的, 請求到來是如何工作的, 響應數(shù)據(jù)對象是如何產(chǎn)生的, url 是如何調(diào)度的, views.py 中定義的方法是何時調(diào)用的, 響應數(shù)據(jù)是如何返回的...另外還提出了一個更好的 url 調(diào)度策略, 如果你有更好的方法, 不忘與大家分享.
我已經(jīng)在 github 備份了 Django 源碼的注釋: Decode-Django, 有興趣的童鞋 fork 吧.
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python?range函數(shù)生成一系列連續(xù)整數(shù)的內(nèi)部機制解析
這篇文章主要為大家介紹了Python?range函數(shù)生成一系列連續(xù)整數(shù)的內(nèi)部機制解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12Python實現(xiàn)定時自動關閉的tkinter窗口方法
今天小編就為大家分享一篇Python實現(xiàn)定時自動關閉的tkinter窗口方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02python監(jiān)控網(wǎng)站運行異常并發(fā)送郵件的方法
這篇文章主要介紹了python監(jiān)控網(wǎng)站運行異常并發(fā)送郵件的方法,涉及Python操作郵件及服務器監(jiān)控的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03Pythonr基于selenium如何實現(xiàn)不同商城的商品價格差異分析系統(tǒng)
這篇文章主要給大家介紹了關于Pythonr基于selenium如何實現(xiàn)不同商城的商品價格差異分析系統(tǒng)的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2022-03-03