litestar-org / litestar

Production-ready, Light, Flexible and Extensible ASGI API framework | Effortlessly Build Performant APIs
https://litestar.dev/
MIT License
5.57k stars 379 forks source link

Enhancement: Use Python stdlib couroutine loop.SendFile for sending files #1250

Open Goldziher opened 1 year ago

Goldziher commented 1 year ago

We should use loop.sendfile (Introduced in Python 3.7) to send files and fallback to chunking. This is significantly more performant.

(edited by @JacobCoffee)

Fund with Polar

Goldziher commented 1 year ago

Update- This appears to be more complex than I assumed. the sendfile method is not implemented on all loops at all. Furthermore, it requires access to the async transport, which is a low level abstraction defined by the asyncio implementation but not anyio.

PROS: dramatically faster file serving using the operating system. CONS: a lot more complexity and only support on asyncio.

I think we have to do this - but it requires significant efforts to implement. An example of an implementation is in aiohttp, specifically here: https://github.com/aio-libs/aiohttp/blob/master/aiohttp/web_fileresponse.py#L83

Additional context: https://github.com/aio-libs/aiohttp/blob/master/aiohttp/web_fileresponse.py#L83

peterschutt commented 1 year ago

specifically here: https://github.com/aio-libs/aiohttp/blob/master/aiohttp/web_fileresponse.py#L83

Additional context: https://github.com/aio-libs/aiohttp/blob/master/aiohttp/web_fileresponse.py#L83

I assume "Additional context:" should be a different link here?