Application objects mainly work:when the server starts:1, in the new app, according to the set URL and callback function Handler encapsulated into Urlspec objectwhen the server is running:2, in the request to come, will httpserver encapsulated HttpRequest incoming _requestdispatcher object, _requestdispatcher object according to the HttpRequest use Urlspec parse match The regular match finds the corresponding RequestHandler and executes its _execute methodApplication Design Benefits1, application itself does not process the data, just encapsulate the URL, parse the URL of the request, distribute to the URL corresponding to the RequestHandler to perform the specific operationThe following is the source analysis, omitted some code, only take the key part display
1 classApplication (httputil. Httpserverconnectiondelegate):2 #inherit from Httputil. Httpserverconnectiondelegate, in fact, Httpserverconnectiondelegate is just a kind of something similar to the agreement, as long as inherit from him can be, convenient other places can call isinstanct to judge, 3 def __init__(Self, Handlers=none, default_host="", Transforms=none, * *settings):4 #complete URL mapping and setting-related settings5 6Self.handlers = []7Self.named_handlers = {} 8Self.settings = Settings#parameters for import settings9 Ten ifHandlers: OneSelf.add_handlers (".*$", handlers)#encapsulating URLs and Handler mappings into Urlspec objects A - ifSelf.settings.get ('Autoreload'):#Set Automatic restart - fromTornadoImportAutoreload the Autoreload.start () - - - defadd_handlers (self, Host_pattern, host_handlers): + #encapsulate URLs and handler into Urlspec - forSpecinchhost_handlers: + ifisinstance (spec, (tuple, list)): A assertLen (spec)inch(2, 3, 4) atSpec = Urlspec (*SPEC)#encapsulate as Urlspec object - handlers.append (spec) - ifSpec.name: - ifSpec.nameinchself.named_handlers: - app_log.warning ( - "multiple handlers named%s; replacing previous value", in spec.name) -Self.named_handlers[spec.name] = Spec#encapsulate the URL and handler insinuate. to + def __call__(self, request): - #here skillfully use the __call__ method, when a application new time, will be passive call, this is after the request information has been received, has been encapsulated into HttpRequest object, execute the user's RequestHandler object's Get, Post Method theDispatcher =_requestdispatcher (self, None) *Dispatcher.set_request (Request)#The encapsulated HttpRequest object (at which time the data has been received) is set into the _requestdispatcher, $ returnDispatcher.execute ()#executes the GET, post method of the user's RequestHandler object, the data processing, and returns
1 class_requestdispatcher (Httputil. Httpmessagedelegate):2 The Get, post, and other methods that are being used to perform user-written RequestHandler objects3 defset_request (self, request):4Self.request = Request#Set Request5Self._find_handler ()#find URL matches according to request6Self.stream_request_body =_has_stream_request_body (Self.handler_class)7 8 def_find_handler (self):9 #TenApp =self.application Onehandlers = App._get_host_handlers (self.request)#Get header information from request A if notHandlers: -Self.handler_class =Redirecthandler -Self.handler_kwargs = Dict (url="/ http"+ App.default_host +"/") the return - forSpecinchHandlers:#traversal URL-matching classes -Match = Spec.regex.match (Self.request.path)#URL Path matching - ifmatch: +Self.handler_class = Spec.handler_class#get the URL match for the custom RequestHandler class that we wrote -Self.handler_kwargs =Spec.kwargs + ifspec.regex.groups: A ifSpec.regex.groupindex: atSelf.path_kwargs =Dict ( - (str (k), _unquote_or_none (v)) - for(k, V)inchmatch.groupdict (). Items ()) - Else: -Self.path_args =[_unquote_or_none (s) - forSinchmatch.groups ()] in return - to + defExecute (self): -Self.handler = Self.handler_class (self.application, Self.request, **self.handler_kwargs)#Get our Custom RequestHandler class instance, pass parameters the * ifSelf.stream_request_body:#Create a future object first $Self.handler._prepared_future =Future ()Panax Notoginseng -Self.handler._execute (Transforms, *self.path_args, **self.path_kwargs)#execute our custom, inherit the _execute function of the RequestHandler class, so finally we write the Get post method, or in the _execute method of our own class call Execute the + returnSelf.handler._prepared_future#returns a custom RequestHandler future object
Tornado Three---Application object of high and Origin code analysis