#32093: Optional Async Middleware without Context Switching
--------------------------------------------+----------------------------
               Reporter:  Andrew Chen Wang  |          Owner:  nobody
                   Type:  New feature       |         Status:  new
              Component:  Utilities         |        Version:  master
               Severity:  Normal            |       Keywords:  middleware
           Triage Stage:  Unreviewed        |      Has patch:  0
    Needs documentation:  0                 |    Needs tests:  0
Patch needs improvement:  0                 |  Easy pickings:  1
                  UI/UX:  0                 |
--------------------------------------------+----------------------------
 Currently, the MiddlewareMixin when using `__acall__` performs
 `sync_to_async` for both `process_request` and `process_response`. I think
 we should allow Middleware implementation to set some property such that:

 {{{
 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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/059.44b80c0c8d2513ddcf451775d15c464c%40djangoproject.com.

Reply via email to