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: 1. Channels is causing middleware to fall out of conformity with Django documentation, which states that middleware’s __init__() is only called *once*, on server startup. 2. Any middleware which performs more-than-trivial work in its __init__() causes massive latency on any incoming request. 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 <https://github.com/django/channels/issues/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-developers+unsubscr...@googlegroups.com. To post to this group, send email to django-developers@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/09941115-c3be-4dc8-871c-0bda1f60a921%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.