diff options
| author | Adam Johnson <me@adamj.eu> | 2023-10-22 23:17:33 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-10-23 09:03:25 +0200 |
| commit | e2922b0d5f18169d1d0115a6db5d2ed8c42d0692 (patch) | |
| tree | 2aa85932d260ab29f505eec49597d1c615fe2462 /django/utils/deprecation.py | |
| parent | 7fcf4f2f0f19c353fe3ee9fe2f6c4baeda4f03c8 (diff) | |
Refs #34118 -- Avoided repeat coroutine checks in MiddlewareMixin.
Diffstat (limited to 'django/utils/deprecation.py')
| -rw-r--r-- | django/utils/deprecation.py | 20 |
1 files changed, 8 insertions, 12 deletions
diff --git a/django/utils/deprecation.py b/django/utils/deprecation.py index 77ff6b1eaa..4d136dfa16 100644 --- a/django/utils/deprecation.py +++ b/django/utils/deprecation.py @@ -100,7 +100,13 @@ class MiddlewareMixin: if get_response is None: raise ValueError("get_response must be provided.") self.get_response = get_response - self._async_check() + # If get_response is a coroutine function, turns us into async mode so + # a thread is not consumed during a whole request. + self.async_mode = iscoroutinefunction(self.get_response) + if self.async_mode: + # Mark the class as async-capable, but do the actual switch inside + # __call__ to avoid swapping out dunder methods. + markcoroutinefunction(self) super().__init__() def __repr__(self): @@ -113,19 +119,9 @@ class MiddlewareMixin: ), ) - def _async_check(self): - """ - If get_response is a coroutine function, turns us into async mode so - a thread is not consumed during a whole request. - """ - if iscoroutinefunction(self.get_response): - # Mark the class as async-capable, but do the actual switch - # inside __call__ to avoid swapping out dunder methods - markcoroutinefunction(self) - def __call__(self, request): # Exit out to async mode, if needed - if iscoroutinefunction(self): + if self.async_mode: return self.__acall__(request) response = None if hasattr(self, "process_request"): |
