question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Channels render MiddlewareNotUsed unusable

See original GitHub issue

I don’t know if this is really a bug, and I needed several hours now to track it down. Please tell me if I just have some misconfig or did not understand basics of channels.

But I think that channels renders middleware - that should be run once - useless. Using django==2.0.7, daphne==2.2.0, channels==2.1.2, Linux/Ubuntu 16.04, Firefox, just setup a simple Django project.

pip install django
pip install django-channels
django-admin startproject one

Create a middleware.py file:

from django.core.exceptions import MiddlewareNotUsed

class OneMiddleware:
    def __init__(self, get_response):
        """One-time configuration and initialization of plugins."""
        print('Middleware started.')
        raise MiddlewareNotUsed

Now add the line

    'one.middleware.OneMiddleware',

to settings.MIDDLEWARE and start it:

cd one
./manage.py migrate
./manage.py runserver

It works: At server start, ‘Middleware started.’ is printed, and with every F5 in the browser nothing more happens in the middleware.

If I now set up channels/routing, which works basically, the “statup-once” effect of my middleware is gone - at every reload of the page, my middleware’s __init__() is called again.

Is this designed/desired functionality? It breaks the only way you can write startup code for Django that should run once. (AppConfig.ready() is no way, and all other ‘unofficial ways’ neither…)

I expected this middleware to run once since MiddlewareNotUsed was raised here, as described in the Django docs.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
majgaardcommented, Aug 24, 2018

I feel that this issue has much larger implications, beyond just rendering MiddlewareNotUsed unusable.

In particular, for middleware like whitenoise which performs some fairly heavy work in its __init__, channels’s approach of initializing a new AsgiHandler for each request (and thus also initializing middleware on each request) is causing serious performance problems for many users. #1121 is a good example of this.

@pvanagtmaal’s suggestion would only initialize middleware once, but I’m unsure of the implications of implementing that. (I’m not super acquainted with channels yet)

Besides middleware, are there any other smart ways of performing certain actions once during startup (which the middleware might be able to access the results of, down-the-line)?

Could we perhaps devise a way to load the middleware chain outside of the AsgiHandler (on defining the ProtocolTypeRouter) and simply inject it into later instantiations of the AsgiHandler? Or is that totally dumb?

0reactions
carltongibsoncommented, Jul 25, 2022

The Channels AsgiHandler will be removed in the next version (v4). Using Django’s implementation going forward, so this issue no longer applies.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Render into a color channels without affecting other channels
For example my frame buffer object has GL_BGRA layout. Now I want to execute some rendering commands which should only change the red...
Read more >
Render Elements for post-production must come!
The most important channel for me actually would be the Material-ID for quick selection in Photoshop (post production). (Imagine to select different layers...
Read more >
Middleware - Django documentation
You don't need to explicitly render responses – responses will be automatically rendered once all template response middleware has been called. Middleware are ......
Read more >
Custom canvas view options in Motion - Apple Support (BN)
In Motion, you can customize various view settings, including zoom level, color channels, rendering options, overlay options, and 3D view layouts.
Read more >
Rendered Insecure: GPU Side Channel Attacks are Practical
They are usually programmed using applica- tion programming interfaces such as OpenGL for 2D/3D graph- ics [17], or WebGL [20] which is usable...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found