Created
April 3, 2023 01:39
-
-
Save zachnicoll/92a9718edbd6a98d96772568fc0e739f to your computer and use it in GitHub Desktop.
FastAPI's (Starlette's) `StreamingReponse` is slow when streaming file-like objects. This gist demonstrates how to quickly stream bytes as 1MB chunks back to the client.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CHUNK_SIZE = 1024 * 1024 # 1MB chunk size | |
def read_bytes_as_chunks(bytesIO: io.BytesIO) -> Generator[bytes, None, None]: | |
# Make sure we're at the start of the buffer | |
bytesIO.seek(0) | |
with bytesIO as b: | |
while chunk := b.read(CHUNK_SIZE): | |
yield chunk | |
@router.get("/") | |
async def download_byte_stream() -> StreamingResponse: | |
my_bytes = BytesIO(b'lots and lots and lots of bytes!') | |
filename = f"my_file.txt" | |
return StreamingResponse( | |
content=read_bytes_as_chunks(my_bytes), | |
media_type="application/x-zip-compressed", | |
headers={"Content-Disposition": f"attachment;filename={zip_filename}"}, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment