After some digging, I was able to find the reason for why adding the Whitenoise middleware to my Channels-enabled project was causing a massive performance drop.
It appears that for every inbound HTTP request, Channels initializes a new AsgiHandler. In turn, this means that the entire middleware stack is being loaded for every HTTP request. See below:
https://github.com/django/channels/blob/master/channels/routing.py#L45-L62
https://github.com/django/channels/blob/master/channels/http.py#L157-L183
This has two consequences:
The issue was diagnosed in:
https://github.com/django/channels/issues/1099
https://github.com/django/channels/issues/1121
In response to the diagnosis in django/channels #1121, Andrew Godwin says:
Hmm, ASGI middleware is loaded every request (#1099) so it's quite possible I made the same mistake with the Django middleware as the handler is loaded every request too. If so, someone should be able to patch that quite easily.
However, I feel that a patch to this issue would be non-trivial to create.
If the AsgiHandler is refactored such that it receives scope upon __call__() rather than on __init__() (and is only initialized once), it no longer conforms to the tests defined in https://github.com/django/asgiref/blob/master/asgiref/testing.py#L17, as they require passing scope for initialization. Thus, the AsgiHandler would no longer fit the shape of an Asgi Application.
I would attempt a fix for this, but I am unsure of how to approach it given the constraints of the problem.
Perhaps someone more knowledgeable about Channels could chime in with their $0.02? I would love to devote some time to fixing this, but need some input before I do.
--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/998d7855-152e-4bc1-b2f2-8fd0f80099b4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.