Skip to content

Better async file handling#2

Open
Chisnet wants to merge 2 commits intomainfrom
better_async_file_handling
Open

Better async file handling#2
Chisnet wants to merge 2 commits intomainfrom
better_async_file_handling

Conversation

@Chisnet
Copy link
Copy Markdown
Member

@Chisnet Chisnet commented Oct 28, 2025

No description provided.

.PHONY: init
init:
python3 -m virtualenv .venv
python3 -m venv .venv
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought [non-blocking]:
Is it trivial to consider uv here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll likely take a look at the build/packaging process at some point in the future, but not now, this approach is definitely more messy to use

)

filename = os.path.basename(file_path)
response['Content-Disposition'] = f'inline; filename="{filename}"'
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought [non-blocking]:
Thinking about headers here. As we know this is static content, are there any headers we could add to help with upstream caching? Or is this not this layers responsibility?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went for least surprise here, since the static filenames aren't hashed in any way (as far as I know) any implementation with caching could cause weirdness. If caching of these files is desired then not using this package and deploying as per Django recommendations on a CDN seems the approach to follow.

async def middleware(request):
if files.get(request.path):
return FileResponse(open(files[request.path], 'rb'))
file_path = files[request.path]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for now but ASGI has two relevant extensions which work a bit like X-SEND-FILE. One allows you to hand over a fd and the server will get the os to write the data, without python getting involved (Zero Copy Send). Even better you can return a path and have the server stream it for you (Path Send). These are both likely to be much faster.

https://asgi.readthedocs.io/en/latest/extensions.html

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good to know, will have to have a look at some point, although not looking to complicate this library too much given it's target usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants