r/FastAPI • u/Impressive_Ad_1738 • Aug 28 '24
Question Reading File Via Endpoint
Hi everyone, I'm an intern and I'm new to FastAPl.
I have an endpoint that waits on a file, to keep it simple, I'll say the endpoint is designed like this
async def read_file (user_file: UploadFile=File (...)): user_file_contents = await user_file.read() return Response(content=user_file_contents)
For smaller files, this is fine and fast, for larger files, about 50MB, it takes about 30 seconds to over a minute before the full document can be received by the endpoint and processed.
Is there a way to speed up this process or make it faster? I really need this endpoint to work under or around seconds or ms
Any help would be appreciated, thank you!
View all comments
Show parent comment
1
u/mincinashu Aug 28 '24 edited Aug 28 '24
You mean the file read() part takes too long? Streaming the response means you're not waiting for the full read, rather you're sending small chunks as soon as you receive them from the OS.
Actually never mind, I see what you mean, the caller is sending a file and then you respond back with the same file?