Skip to content

Instantly share code, notes, and snippets.

@zachnicoll
Created April 3, 2023 01:39
Show Gist options
  • Save zachnicoll/92a9718edbd6a98d96772568fc0e739f to your computer and use it in GitHub Desktop.
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.
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