Replies: 3 comments
-
|
Agree that it would be useful and sometimes necessary to be able to use an UploadFile in a background task. For me it is necessary as customers sometimes upload large files that take a long time to process so cannot hold up response waiting for that processing to finish. Also would like to avoid deep copying the file. |
Beta Was this translation helpful? Give feedback.
-
|
What I did eventually for my case was to not use But what I am guessing is that FastAPI could only guarantee that this I think FastAPI allowing this to happen pre Maybe there should be an explicit method on I am no expert in this and I could very well be misunderstanding how FastAPI actually handles uploaded files. So sorry for any mistakes, but I hope it leads to more discussions. |
Beta Was this translation helpful? Give feedback.
-
I have run into the same issue. Thank you for this workaround. I think that the documentation on the behavior w.r.t. UploadFile and Background task interactions could be clearer. I would be happy to help, but I may also be misunderstanding the behavior here. Regardless if the behavior is good or not, I think this behavior should be documented somewhere. In-memory file manipulation + background tasks seems like it would be a fairly common pattern?
What is the reason given for your friend saying this? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
As discussed in the issue #10857. The file used to be open when executing the
do_sth_with_the_filefunction. But it gets closed in recent versions of FastAPI.I created a workaround using
copy.deepcopy:As a friend of mine pointed out, maybe it is not a good idea to process the file in background at all. But anyway, the result was a bit surprising to me. Maybe it is worth mentioning in the documentation?
I'd like to help anyone who would stumble upon the same issue. But I don't know if coping is the way to go, or doing something completely different.
Operating System
Linux
Operating System Details
No response
FastAPI Version
0.108.0
Pydantic Version
2.5.3
Python Version
Python 3.12.1
Additional Context
#10857
Beta Was this translation helpful? Give feedback.
All reactions