Just finished reading both bug reports and I see the file loader lookup in 
whitenoise. This explains a lot actually. We are still on Channels 1, and 
when we tried an upgrade to Channels 2, but encountered terrible 
performance at load and had to undo it.

On Tuesday, August 28, 2018 at 8:00:17 AM UTC-4, John Obelenus wrote:
>
> Can I ask a dumb question? Why did you only discover this with adding 
> Whitenoise, and not the default/common middleware stack that comes in 
> django? Asking that question in another way: What is specific to whitenoise 
> that this constant loading of middleware causes such a perf hit? This 
> question in no way implies this should not be fixed, its just my curiosity 
> (I'm in the exact same situation of running channels and whitenoise)
>
> On Monday, August 27, 2018 at 12:46:15 PM UTC-4, Carl-Philip Majgaard 
> wrote:
>>
>> 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/998d7855-152e-4bc1-b2f2-8fd0f80099b4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to