whitenoise
whitenoise copied to clipboard
Considering ASGI.
Okay, I'm going to punt this one out there.
Andrew Godwin's latest work on the ASGI spec, means that there really is now an asyncio-based interface for Python webservers.
I'm planning to shortly be releasing a new release of uvicorn
, at which point there will be two production-ready ASGI servers. (We already have daphne
available.)
I'll also be releasing a new version of apistar
which will present either WSGI or ASGI interfaces, so there will be at least two ASGI web application frameworks. (We already have Django Channels)
I already use whitenoise
for serving static files with apistar
, but it's not ideal under ASGI, because we necessarily need to run whitenoise behind as WSGI-to-ASGI adapter, which means that:
- We're not actually running asyncio code for disk operations.
- We're not able to stream response data, instead everything gets buffered at the adapter layer.
- There's no support for
send_file
. (This isn't currently addressed in the ASGI spec, so that's more complicated to address.)
This may well be premature, given the early nature of ASGI, but I'm wondering if you'd consider whitenoise-as-an-ASGI-middleware. If we use sync file operations on load, then I think ASGI support could be addressed with...
- A
WhiteNoise
subclass,ASGIWhiteNoise
that overrides__call__
, andserve
. - Changing
get_response
and file iteration in the asyncio case, to use something like aiofiles. - Considering how a
sendfiles
extension to ASGI might look.
I'll likely be tackling some of this as part of my work on apistar, but figured I'd also raise this here just to get an idea of if you'd be interested in seeing ASGI supported directly in whitenoise?
Hi Tom, thanks for raising this. Yes, I'd definitely be interested in supporting native ASGI in WhiteNoise. My hope is that it won't actually involve a lot of extra code. Fundamentally, WhiteNoise takes a request path, HTTP method, and headers and returns a status code, response headers, and an open file handle. There's a bit of adaptor code to get this to work with the WSGI and Django middleware specs. So hopefully the ASGI adaptor code will be equally small, but I haven't looked into this in detail yet.
I don't know exactly when I'll get time to look at this (the WhiteNoise 4.0 release is already massively behind due to work and family commitments) but very happy to review patches or discuss options.
I've put together #181, to demonstrate the scope of changes.
The most obvious problem would be that using async
/await
syntax makes the codebase no longer be 2.7 compatible.
In version 5.0 python 2.7 support is dropped so is this a option again?
Yes, absolutely. It's "just" a case of finding the time.
@evansd see my comment about possibly moving this project to JazzBand to get the community more involved:
https://github.com/evansd/whitenoise/pull/261#issuecomment-825016490
I'm using WhiteNoise with Uvicorn (ASGI) and it works fine in a Docker container. Are there any caveats to it?