aio-libs / aio-libs/aiohttp

Support FileResponse.prepare() to be re-entrant just as StreamResponse.prepare().

Đang mở
#4,647 2 bình luận 1 reaction 0 người được giao Xem trên GitHub
enhancement
Ngôn ngữ chính
Python
Star
16.5k
Fork
2.4k
Merge trung bình
20 giờ 10 phút
Pull request đã merge (30 ngày)
221

Mô tả

🐣 **Is your feature request related to a problem? Please describe.**

There is no way to know when a file is sent if the file is required to be removed after consumed.

As the docs of FileResponse reads,
> The actual body sending happens in overridden **prepare()**.

I tried to call `await file_resp.prepare()` myself in a handler function just like what we are used to do with StreamResponse, and then the file is removed after leaving the handler.

An Exception was raised after leaving the handler since `prepare()` was called again here without checking if the response had already been prepared.
https://github.com/aio-libs/aiohttp/blob/0dbf93726278a0b527ef56212600d2f2aedc7638/aiohttp/web_protocol.py#L461

Although I can use a StreamResponse instead of a FileResponse to tackle this problem, doing the same thing as what [FileResponse._sendfile_fallback()](https://github.com/aio-libs/aiohttp/blob/0dbf93726278a0b527ef56212600d2f2aedc7638/aiohttp/web_fileresponse.py#L174) does. I think it's the responsibility of a FileResponse to send a file (though a file to be removed soon), also to avoid code duplication.

💡 **Describe the solution you'd like**

Just like what `StreamResponse.prepare()` does, I think the same check should also be implemented in `FileResponse.prepare()`.

https://github.com/aio-libs/aiohttp/blob/0dbf93726278a0b527ef56212600d2f2aedc7638/aiohttp/web_response.py#L347-L350

❓ **Describe alternatives you've considered**

Provide any way for developers to know a file is sent, maybe an `asyncio.Event` or a signal.

---

I know there are tons of issues here, but I think it's an improvement for the library to be more comprehensive.
Thank you!

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.