After inspecting code, I figured out that this was because the
`django.core.handlers.asgi.ASGIHandler` class provides a `send_response`
method which is ''natively async''. This method subsequently fires the
`request_finished` signal and since this happens from within an async
context, Django complains and rightly so.
So the issue here is that this behaviour about the `request_finished`
signal hasn't been documented and so it might come as a surprise to people
when they try to run their code using an ASGI server. Also, any such ORM
queries made from a callback hooked to the `request_started` signal works
just fine without any modifications.
The solution to this problem could be as follows:
1. The synchronous blocking calls made from within the callback hooked to
`request_finished` could be made async using the sync_to_async adapter and
this has to be documented appropriately.
(OR)
2. The `send_response` method could be made natively synchronous and
converted into an async callable using the sync_to_async adapter wherein
it would run in a background thread without blocking the event loop but
this could have performance implications.
If we can finalise on one of the above two or an even better one, I could
send a patch for the same.
--
Ticket URL: <https://code.djangoproject.com/ticket/32447>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* cc: Aditya N (added)
* has_patch: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/32447#comment:1>
* type: Uncategorized => Bug
--
Ticket URL: <https://code.djangoproject.com/ticket/32447#comment:2>
* status: new => closed
* resolution: => wontfix
Comment:
Thanks for this ticket, however Django 3.0 is in extended support so it
doesn't receive bugfixes anymore (except security fixes and data loss
bugs). This issue was fixed in fc0fa72ff4cdbf5861a366e31cb8bbacd44da22d. I
would strongly recommend to use Django 3.1+ which supports a fully
asynchronous request path.
--
Ticket URL: <https://code.djangoproject.com/ticket/32447#comment:3>