如何知道哪些 django 中间件是异步启用的,哪些不是?

How to know which django middlewares are asynchronous enabled and which are not?

To see what middleware Django has to adapt, you can turn on debug logging for the django. request logger and look for log messages about “Synchronous middleware … adapted” .

我一直在尝试做同样的事情,但没有任何运气。

这是我的 settings.py 文件:

LOGGING = {  
 'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        },
    },
    'root': {
        'handlers': ['console'],
        'level': 'DEBUG',
    },
    'loggers': {
        'django.request': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}

即使我已经设置了 LOGGING 变量,我也没有得到文档中提到的输出。

Starting server at tcp:port=8000:interface=127.0.0.1
HTTP/2 support not enabled (install the http2 and tls Twisted extras)
Configuring endpoint tcp:port=8000:interface=127.0.0.1
Listening on TCP address 127.0.0.1:8000
HTTP b'GET' request for ['127.0.0.1', 42684]
HTTP 200 response started for ['127.0.0.1', 42684]
HTTP close for ['127.0.0.1', 42684]
HTTP response complete for ['127.0.0.1', 42684]
127.0.0.1:42684 - - [22/Mar/2022:12:11:47] "GET /admin/" 200 3550
HTTP b'GET' request for ['127.0.0.1', 42684]
HTTP 200 response started for ['127.0.0.1', 42684]
HTTP close for ['127.0.0.1', 42684]
HTTP response complete for ['127.0.0.1', 42684]
127.0.0.1:42684 - - [22/Mar/2022:12:11:48] "GET /admin/core/user/" 200 9028
HTTP b'GET' request for ['127.0.0.1', 42684]
HTTP 200 response started for ['127.0.0.1', 42684]
HTTP close for ['127.0.0.1', 42684]
HTTP response complete for ['127.0.0.1', 42684]
127.0.0.1:42684 - - [22/Mar/2022:12:11:48] "GET /admin/jsi18n/" 200 3343

当我运行使用达芙妮服务器时,

daphne project_name.asgi:application

命令。 任何人都可以帮助我获得有关所有中间件是异步的而哪些不是异步的输出。

我尝试制作一个视图并通过浏览器向它发出请求,但它似乎没有打印任何中间件。虽然,已经有可能正在使用中间件的管理模型,但没有诸如“同步中间件...已修改”之类的输出。

我在问题中使用的记录器非常正确地查看了哪些中间件是异步启用的。但问题是我想知道为什么 Django 提供的中间件没有在这里显示哪些是异步启用的,哪些不是。 所以,我试着自己做了一个中间件来检查这个特性。

middleware.py

class SimpleMiddleware(MiddlewareMixin):
    def __init__(self, get_response):
        self.async_capable = False
        self.get_response = get_response
        # One-time configuration and initialization.

    def __call__(self, request):
        # Code to be executed for each request before
        # the view (and later middleware) are called.

        response = self.get_response(request)

        # Code to be executed for each request/response after
        # the view is called.

        return response

现在,如果您在 settings.py 文件和 运行 服务器中添加此中间件,那么您会看到日志会按预期说明此中间件。