Opened 3 years ago
Last modified 3 years ago
#33716 closed New feature
async get_response can be a regular function too — at Version 6
| Reported by: | abetkin | Owned by: | nobody |
|---|---|---|---|
| Component: | Uncategorized | Version: | 4.0 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Here is what we have in 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 asyncio.iscoroutinefunction(self.get_response):
# 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
This checks if the next middleware is a coroutine, and if not fallbacks to sync mode. However, we already mark all async-capable middleware as such, why the redundancy?
A common usecase that is currently not supported:
def MyMiddleware(get_response):
def middleware(request):
# Do some stuff with request that does not involve I/O
request.vip_user = True
return get_response(request)
return middleware
MyMiddleware.async_capable=True
middleware(request) will return the response in sync case and a coroutine in the async case, despite being a regular function (because get_response is a coroutine function in the latter case).
So I propose to remove the redundant _async_check
Github project to see the error: https://github.com/pwtail/django_bug
Change History (7)
by , 3 years ago
comment:1 by , 3 years ago
| Description: | modified (diff) |
|---|
comment:2 by , 3 years ago
| Description: | modified (diff) |
|---|
comment:3 by , 3 years ago
| Description: | modified (diff) |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
| Type: | Uncategorized → Bug |
comment:4 by , 3 years ago
| Resolution: | → invalid |
|---|---|
| Status: | new → closed |
comment:5 by , 3 years ago
| Severity: | Release blocker → Normal |
|---|
comment:6 by , 3 years ago
| Description: | modified (diff) |
|---|---|
| Resolution: | invalid |
| Status: | closed → new |
| Summary: | Async-capable middleware is not async-capable → async get_response can be a regular function too |