Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions django/utils/deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,16 +315,32 @@ async def __acall__(self, request):
Async version of __call__ that is swapped in when an async request
is running.
"""

# Resolve sync_to_async adapters once, outside the request path.
# This avoids repeated construction of the closure on every call.
if not hasattr(self, "_sync_process_request"):
if hasattr(self, "process_request"):
self._sync_process_request = sync_to_async(
self.process_request,
thread_sensitive=True,
)
else:
self._sync_process_request = None

if not hasattr(self, "_sync_process_response"):
if hasattr(self, "process_response"):
self._sync_process_response = sync_to_async(
self.process_response,
thread_sensitive=True,
)
else:
self._sync_process_response = None

response = None
if hasattr(self, "process_request"):
response = await sync_to_async(
self.process_request,
thread_sensitive=True,
)(request)

if self._sync_process_request is not None:
response = await self._sync_process_request(request)
response = response or await self.get_response(request)
if hasattr(self, "process_response"):
response = await sync_to_async(
self.process_response,
thread_sensitive=True,
)(request, response)
if self._sync_process_response is not None:
response = await self._sync_process_response(request, response)
return response