Django 如何保证中间件的 __init__ 只被调用一次?
How does Django guarantee a middleware's __init__ to be called only once?
Django 的文档:
Unlike the process_* methods which get called once per request, __init__
gets called only once, when the Web server responds to the first request.
Django如何保证中间件对象只初始化一次? Web 服务器可以启动多个 python 解释器实例来处理请求,对吗?
这意味着每个解释器实例一次。注意实现:
class WSGIHandler(base.BaseHandler):
# ...
def __call__(self, environ, start_response):
# ...
if self._request_middleware is None:
self.load_middleware()
您可以阅读 django/core/handlers/wsgi.py
and django/core/handlers/base.py
(尤其是 Basehandler.load_middleware
)了解更多详情。
Django 的文档:
Unlike the process_* methods which get called once per request,
__init__
gets called only once, when the Web server responds to the first request.
Django如何保证中间件对象只初始化一次? Web 服务器可以启动多个 python 解释器实例来处理请求,对吗?
这意味着每个解释器实例一次。注意实现:
class WSGIHandler(base.BaseHandler):
# ...
def __call__(self, environ, start_response):
# ...
if self._request_middleware is None:
self.load_middleware()
您可以阅读 django/core/handlers/wsgi.py
and django/core/handlers/base.py
(尤其是 Basehandler.load_middleware
)了解更多详情。