(代碼縮進(jìn)有點(diǎn)問題? 大家可以看源碼)
tornado有許多關(guān)于如何處理路由列表的源碼分析的博客维费,關(guān)鍵在與調(diào)用了Application.__call__函數(shù),然后遍歷路由列表汉买,取出對應(yīng)的處理類幔妨,由于處理類都是RequestHandler類榨惠,調(diào)用的是父類的_excute()進(jìn)行響應(yīng)處理厌衙,我們要了解的是__call__函數(shù)和什么時(shí)候調(diào)用了__call__函數(shù)
def __call__(self, request):
"""Called by HTTPServer to execute the request."""
transforms = [t(request) for t in self.transforms]
handler = None
args = []
kwargs = {}
handlers = self._get_host_handlers(request)
if not handlers:
handler = RedirectHandler(
self, request, url="http://" + self.default_host + "/")
else:
for spec in handlers:
match = spec.regex.match(request.path)
if match:
handler = spec.handler_class(self, request, **spec.kwargs)
if spec.regex.groups:
# None-safe wrapper around url_unescape to handle
# unmatched optional groups correctly
def unquote(s):
if s is None:
return s
return escape.url_unescape(s, encoding=None,
plus=False)
# Pass matched groups to the handler.? Since
# match.groups() includes both named and unnamed groups,
# we want to use either groups or groupdict but not both.
# Note that args are passed as bytes so the handler can
# decide what encoding to use.
if spec.regex.groupindex:
kwargs = dict(
(str(k), unquote(v))
for (k, v) in match.groupdict().items())
else:
args = [unquote(s) for s in match.groups()]
break
if not handler:
if self.settings.get('default_handler_class'):
handler_class = self.settings['default_handler_class']
handler_args = self.settings.get(
'default_handler_args', {})
else:
handler_class = ErrorHandler
handler_args = dict(status_code=404)
handler = handler_class(self, request, **handler_args)
# 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.settings.get("compiled_template_cache", True):
with RequestHandler._template_loader_lock:
for loader in RequestHandler._template_loaders.values():
loader.reset()
if not self.settings.get('static_hash_cache', True):
StaticFileHandler.reset()
handler._execute(transforms, *args, **kwargs)
return handler
當(dāng)http_server.listen(options.port)啟動(dòng)監(jiān)聽的時(shí)候兜粘,程序會accept socket.詳見netutil.add_accept_handler函數(shù),我們要注意傳遞的第一個(gè)參數(shù)_handle_connection是什么砸泛,是一個(gè)函數(shù)十籍,這里先不具體看函數(shù),我們看看到add_accept_handler后是怎么處理這些參數(shù)的
def listen(self, port, address=""):
if self.io_loop is None:
self.io_loop = IOLoop.current()
for sock in sockets:
self._sockets[sock.fileno()] = sock
add_accept_handler(sock, self._handle_connection,
io_loop=self.io_loop)
def _handle_connection(self, connection, address):
if self.ssl_options is not None:
assert ssl, "Python 2.6+ and OpenSSL required for SSL"
try:
connection = ssl_wrap_socket(connection,
self.ssl_options,
server_side=True,
do_handshake_on_connect=False)
except ssl.SSLError as err:
if err.args[0] == ssl.SSL_ERROR_EOF:
return connection.close()
else:
raise
except socket.error as err:
if err.args[0] in (errno.ECONNABORTED, errno.EINVAL):
return connection.close()
else:
raise
try:
if self.ssl_options is not None:
stream = SSLIOStream(connection, io_loop=self.io_loop, max_buffer_size=self.max_buffer_size)
else:
stream = IOStream(connection, io_loop=self.io_loop, max_buffer_size=self.max_buffer_size)
self.handle_stream(stream, address)
except Exception:
app_log.error("Error in connection callback", exc_info=True)
#這是add_accept_handler(sock, self._handle_connection,io_loop=self.io_loop)
def add_accept_handler(sock, callback, io_loop=None):
"""Adds an `.IOLoop` event handler to accept new connections on ``sock``.
When a connection is accepted, ``callback(connection, address)`` will
be run (``connection`` is a socket object, and ``address`` is the
address of the other end of the connection).? Note that this signature
is different from the ``callback(fd, events)`` signature used for
`.IOLoop` handlers.
"""
if io_loop is None:
io_loop = IOLoop.current()
def accept_handler(fd, events):
while True:
try:
connection, address = sock.accept()
except socket.error as e:
# EWOULDBLOCK and EAGAIN indicate we have accepted every
# connection that is available.
if e.args[0] in (errno.EWOULDBLOCK, errno.EAGAIN):
return
# ECONNABORTED indicates that there was a connection
# but it was closed while still in the accept queue.
# (observed on FreeBSD).
if e.args[0] == errno.ECONNABORTED:
continue
raise
callback(connection, address)
io_loop.add_handler(sock.fileno(), accept_handler, IOLoop.READ)
add_accept_handler(sock, callback, io_loop=None)函數(shù)接受request請求唇礁,調(diào)用了callback(connection,address)函數(shù)并且給IO事件循環(huán)注冊了一個(gè)事件勾栗,我們應(yīng)該知道callback()函數(shù)的,就是傳遞過來的參數(shù)_handle_connection()再看看這個(gè)函數(shù)做了什么處理分析不管前面做了什么處理盏筐,有一句是要執(zhí)行的self.handle_stream(stream, address)围俘,原來調(diào)用了HttpServer的
def handle_stream(self, stream, address):
HTTPConnection(stream, address, self.request_callback,
self.no_keep_alive, self.xheaders, self.protocol)
調(diào)用了HTTPConnection對象,很簡單机断,應(yīng)該只調(diào)用了構(gòu)造方法
看看Httpserver的構(gòu)造方法
def __init__(self, request_callback, no_keep_alive=False, io_loop=None,
xheaders=False, ssl_options=None, protocol=None, **kwargs):
self.request_callback = request_callback
self.no_keep_alive = no_keep_alive
self.xheaders = xheaders
self.protocol = protocol
TCPServer.__init__(self, io_loop=io_loop, ssl_options=ssl_options,
**kwargs)
def handle_stream(self, stream, address):
HTTPConnection(stream, address, self.request_callback,
self.no_keep_alive, self.xheaders, self.protocol)
request_callback是什么http_server = tornado.httpserver.HTTPServer(Application())這里清楚了是Application()楷拳,分析
def __init__(self, stream, address, request_callback, no_keep_alive=False,
xheaders=False, protocol=None):
self.stream = stream
self.address = address
# Save the socket's address family now so we know how to
# interpret self.address even after the stream is closed
# and its socket attribute replaced with None.
self.address_family = stream.socket.family
self.request_callback = request_callback
self.no_keep_alive = no_keep_alive
self.xheaders = xheaders
self.protocol = protocol
self._clear_request_state()
# Save stack context here, outside of any request.? This keeps
# contexts from one request from leaking into the next.
self._header_callback = stack_context.wrap(self._on_headers)
self.stream.set_close_callback(self._on_connection_close)
self.stream.read_until(b"\r\n\r\n", self._header_callback)
self._header_callback = stack_context.wrap(self._on_headers)這一句很關(guān)鍵,
def _on_headers(self, data):
#省略******
self.request_callback(self._request)
return
self.request_callback(self._request)前面說了吏奸,.request_callback=Application()所以request_callback(self._request) = Application()(self._request)類被當(dāng)做函數(shù)調(diào)用欢揖,所以__call__函數(shù)被調(diào)用了,就有了路由列表處理的操作奋蔚,比較繞啊