From 33401cba9317de2487c31ffdd1a5a51ecfbc0248 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Tue, 21 Dec 2021 07:41:29 +0000 Subject: [PATCH] Optimized MiddlewareMixin coroutine check. --- django/utils/deprecation.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/django/utils/deprecation.py b/django/utils/deprecation.py index f64bc3c6fb..48209bcdf1 100644 --- a/django/utils/deprecation.py +++ b/django/utils/deprecation.py @@ -112,10 +112,12 @@ class MiddlewareMixin: # Mark the class as async-capable, but do the actual switch # inside __call__ to avoid swapping out dunder methods self._is_coroutine = asyncio.coroutines._is_coroutine + else: + self._is_coroutine = None def __call__(self, request): # Exit out to async mode, if needed - if asyncio.iscoroutinefunction(self.get_response): + if self._is_coroutine: return self.__acall__(request) response = None if hasattr(self, 'process_request'):