view is not always executed

request will be recorded by the middleware and executed in async view, but in some cases, it will not be executed in view.

Settings:

Python: 3.10.12

Django: 4.2.1

middleware settings:

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "corsheaders.middleware.CorsMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
    ...
    "utils.middleware.RequestMiddleware",
]

RequestMiddleware:

class RequestMiddleware(MiddlewareMixin):
    def process_view(self, request, view_func, view_args, view_kwargs):
        req_str = "reqeust user:{} request method:{} request path:{} request body:{}".format(
            request.user,
            request.META["REQUEST_METHOD"],
            request.path,
            request.body
        )
        logger.info(req_str)
        return None

    def process_response(self, request, response):
        data = response.content
        data = data.decode("utf-8")
        logger.info("response status code:{} data:{}".format(response.status_code, data))
        return response

    def process_exception(self, request, exception):
        logger.error(traceback.format_exc())

views.py:

async def report(request):
    my_views.delay()  # celery task
    return send_message({"result": 0})

Welcome @AhaozZ !

Can you be more specific and provide more details about the issue you’re facing here? I’m not sure I understand what you’re asking or what information you’re looking for from us.

Hi Ken!
In prod environment, RequestMiddleware can log information for each request, but having an async view does not always work. Most of the time, it can work normally, but sometimes it may not work. I cannot reproduce this situation when using jemter for testing in the testing environment.
I try to log something in this view:

async def report(request):
    logger.info(f"request:{request.body} start work")
    my_views.delay()  # celery task
    return send_message({"result": 0})

There are some requests in the log that have not been recorded.