tornado study notes 18 _RequestDispatcher Request Dispatcher
18.1 Constructors
Definition:
def __init__(self, application, connection):
Parameters:
application: Application object.
connection: Request a connection, an instance of HTTP1Connection.
Implementation analysis:
It is to initialize the properties of this class. Such as chunks, handler_class (RequestHandler processing class), handler_kwargs (processing class parameters), path_args, path_kwargs, etc.
18.2 HTTPMessageDelegate interface implementation
1.2.1 header_received
Definition
def headers_received(self, start_line, headers):
Implementation analysis:
Instantiate the HTTPServerRequest object, call the set_request method, and find the matching request handler class (RequestHandler) in the set_request method. If the request processing class implements the _stream_request_body method, the execute method is called directly.
1.2.2 data_received
Definition
def data_received(self, data):
Implementation process:
If RequestHandler implements the _stream_request_body method, call the handler's data_received method, if not, add the data block to chunks.
1.2.3 finish
This method is called when the message data block has been received. The implementation of this method is to connect the data blocks, and then call the _parse_body method of HTTPServerReuqest to parse the body message. Then call the execute method.
18.3 Other methods
1.3.1 _find_handler
This method is very important and one of the core methods. Implement the matching of Application's configuration properties and request paths, and find the matching RequestHandler.
Implementation process:
(1) Call the _get_host_handlers method of Application to obtain the matching set of handlers;
(2) If no suitable handlers are matched, set handler_class to RedirectHandler, set handler_kwargs, and return.
(3) If the appropriate handlers are matched, loop each element URLSpec in the handlers to determine whether the path regular expression in it matches the request path.
(4) If it matches the appropriate RequestHandler, set handler_class and handler_kwargs, and then set path_kwargs
(5) If the RequestHandler is not matched, judge whether the Application has set the default_handler_class option, and set the handler_class as its value. If the default_handler_class option is not set, the handler_class attribute is set to ErrorHandler, and the status code is set to 404 error, that is, not found error.
18.3.2 execute
This method is very core and very important. When the request information is processed (after calling the finish method), the execute method will be executed. Methods as below:
def execute(self): # If template cache is disabled (usually in the debug mode),
# re-compile templates and reload static files on every
# request so you don't need to restart to see changes
if not self.application.settings.get("compiled_template_cache", True):
with RequestHandler._template_loader_lock:
for loader in RequestHandler._template_loaders.values():
loader.reset()
if not self.application.settings.get('static_hash_cache', True):
StaticFileHandler.reset()
self.handler = self.handler_class(self.application, self.request,
**self.handler_kwargs)
transforms = [t(self.request) for t in self.application.transforms]
if self.stream_request_body:
self.handler._prepared_future = Future()
# Note that if an exception escapes handler._execute it will be
# trapped in the Future it returns (which we are ignoring here,
# leaving it to be logged when the Future is GC'd).
# However, that shouldn't happen because _execute has a blanket
# except handler, and we cannot easily access the IOLoop here to
# call add_future (because of the requirement to remain compatible
# with WSGI)
f = self.handler._execute(transforms, *self.path_args,
**self.path_kwargs)
# If we are streaming the request body, then execute() is finished
# when the handler has prepared to receive the body. If not,
# it doesn't matter when execute() finishes (so we return None)
return self.handler._prepared_future
The implementation process is described as follows:
(1) Determine whether the application is set to True for compiled_template_cache. If not, reset the template loader. It is equivalent to resetting the template loader if the compiled template is not cached.
(2) Determine whether application is set to True for static_hash_cache. If not, call the reset method of StaticFileHandler. It is equivalent to calling the reset method if the static files of the website are not cached.
(3) Initialize a custom RequestHanlder according to the handler_class attribute set by the _find_handler method
(4) Calling the _exucute method of requestHandler is to call the corresponding method. The key is the get method, either the post method, or other supported http methods. Please refer to the RequestHandler class for specific implementation details.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.