The Request._get_file_stream
method is used to get file-like objects for storing file uploads. The default implementation uses SpooledTemporaryFile
with a limit of 500 KiB, which keeps the data in memory before writing to a regular temporary file.
Subclass and override the method to always return BytesIO
. Tell the Flask app to use that class instead of the default one.
from flask.wrappers import Request
class MemoryRequest(Request):
def _get_file_stream(self, total_content_length, content_type, filename, content_length):
return BytesIO()
app.request_class = MemoryRequest
You could also change the memory threshold by returning SpooledTemporaryFile(max_size=100_000_000, mode="rb+")
(100 MB for example).
Memory is a much more limited shared resource than disk space, so it usually wouldn't be a good idea to store everything in memory. There should be no reason not to use temporary files.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…