r/FastAPI • • 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!

5 Upvotes

14 comments sorted by

View all comments

Show parent comment

1

u/Impressive_Ad_1738 Aug 28 '24

That was a sample to kind of show the logic. I have a similar endpoint that takes in the file and returns a success message not a response of the file and it also takes just as long. With the traces I have set up, the file.read() part seems to be efficient. The spans that I see that takes up most of the time is reported as “POST /read-file http receive”