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.

Running whitenoise behind a WSGI-to-ASGI adapter

See original GitHub issue

I’m about to deploy a django app. I was originally going to use uvicorn with Django 3.0, but after seeing whitenoise doesn’t work with asgi servers yet was thinking of just reverting back to wsgi.

I just came across @tomchristie’s pull request and he mentions he’s able to (non-ideally) get it working with a wsgi-to-asgi adapter. I was wondering if there’s any additional information on how to get that working.

Ideally I’d like to just swap that out whenever whitenoise goes asgi, instead of reverting from uvicorn to gunicorn.

Thanks for the continued work on a great project!

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
kmichel2commented, Aug 24, 2020

Hi, I’ve had success in using WhiteNoiseMiddleware in an async context in Django, but I noticed that WhiteNoiseMiddleware does not inherit from MiddlewareMixin.

The consequence is that WhiteNoiseMiddleware does not get the full async compatibility layer from Django 3.1.

It would not be a full async support but maybe implementing sync_capable=True and being able to call an async get_response (and be called as async) could be a useful improvement of WhiteNoiseMiddleware.

It would reduce the time spent in the sync emulation layer (which is a separate thread with essentially zero concurrency because Django calls sync_to_async with the thread_sensitive=True option) and allow async views to pass through directly when not serving a static file request.

Copying the behaviour of MiddlewareMixin, it could roughly look like this:

class AsyncWhitenoiseMiddleware(WhiteNoiseMiddleware):
    sync_capable = True
    async_capable = True

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._async_check()

    def _async_check(self):
        if asyncio.iscoroutinefunction(self.get_response):
            self._is_coroutine = asyncio.coroutines._is_coroutine

    def __call__(self, request):
        if asyncio.iscoroutinefunction(self.get_response):
            return self.__acall__(request)
        else:
            return super().__call__(request)

    async def __acall__(self, request):
        response = self.process_request(request)
        if response is None:
            response = await self.get_response(request)
        return response

It’s probably better to inherit from MiddlewareMixin instead of duplicating code.

In the previous code, process_request is called directly, which means doing disk IO in the main async thread. This is not ideal but probably not awful, I’d guess static files are very likely to be quickly available from the system cache.

4reactions
evansdcommented, May 20, 2020

Hi, yes DjangoWhitenoise acts as standard Django middleware so should run fine via the compatibility layer. I’d love to get native ASGI integration into Whitenoise but super busy with the medical day job at the moment and ENOTIME 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using WhiteNoise with any WSGI application
On initialization, WhiteNoise walks over all the files in the directories that have been added (descending into sub-directories) and builds a list of...
Read more >
whitenoise
I have what I believe is an uncommon setup: during deploy I have both new and old application servers working simultaneously (using k8s),...
Read more >
A Guide to ASGI in Django 3.0 and its Performance
A blog by Arun Ravindran, author and speaker, about Python, Django and other curious things.
Read more >
How to use the whitenoise.django.DjangoWhiteNoise ...
To help you get started, we've selected a few whitenoise.django.DjangoWhiteNoise examples, based on popular ways it is used in public projects.
Read more >
Issues with poetry when deploying Django application
Hi, I am following the “Getting Started With Django on Render” tutorial and all was going well until I got to “Configure Django...
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