{{{
class MiddlewareMixin:
# Already defined
sync_capable = True
async_capable = True
# New
native_async = False
async def __acall__(self, request):
"""
Async version of __call__ that is swapped in when an async request
is running.
"""
response = None
if hasattr(self, 'process_request'):
if self.native_async:
response = await self.process_request(request)
else:
response = await sync_to_async(
self.process_request,
thread_sensitive=True,
)(request)
response = response or await self.get_response(request)
if hasattr(self, 'process_response'):
if self.native_async:
response = self.process_response(request, response)
else:
response = await sync_to_async(
self.process_response,
thread_sensitive=True,
)(request, response)
return response
}}}
in order to avoid context switching which will improve performance.
--
Ticket URL: <https://code.djangoproject.com/ticket/32093>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.