Memory consumption spikes when an api endpoint with file upload capability is hit. #5778
Unanswered
athujoshi24
asked this question in
Q&A
Replies: 1 comment 2 replies
-
|
Hey @athujoshi24, been through this exact thing with Flask + large uploads. The main problem is Flask loads the whole file into memory before your function even runs. A couple things that help: 1. Force Flask to write to disk instead of memory from flask import Flask, Request
from tempfile import TemporaryFile
class StreamingRequest(Request):
max_content_length = 16 * 1024
def _get_file_stream(self, total_content_length, content_type, filename=None, content_length=None):
return TemporaryFile('w+b')
app = Flask(__name__)
app.request_class = StreamingRequest
app.config['MAX_CONTENT_LENGTH'] = 450 * 1024 * 10242. Read the file in chunks inside your function def upload_data(**params):
data = params["data"]
ticket = _get_ticket(params)
storage = Storages(data_path="/path/to/storage")
data_path = storage.ticket_path(ticket, params["dataType"])
CHUNK_SIZE = 1024 * 1024 # 1MB
with open(data_path, 'wb') as f:
while True:
chunk = data.stream.read(CHUNK_SIZE)
if not chunk:
break
f.write(chunk)
return None, HTTPStatus.CREATED3. Tune Gunicorn You're running 5 UvicornWorker workers with a 3GB limit. Each worker could load a big upload and then Kubernetes kills it with the OOM killer. Try:
If it still hits the limit, bump the pod memory to like 5GB. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I have an API application, created using
connexionframework, which usesFlaskAppconfiguration. This app exposes an endpoint where users/consumers can upload artifacts(ranging from 2.5 MB to 400 MB) and they will be then upload to an internal storage platform.The pain point is, when the user selects a larger data file(~320 MB) to upload, the application often runs into OOM issue and eventually user receives 502 status response.
Running
dmesgcommand inside containers show the gunicorn thread being killed byoom_killerIt would be of great help if someone here could guide in avoiding such OOM issues for Flask Applications.
API Specifaction
Python function
Pipfile
Application startup command
Filtered logs
System info:
In a thread regarding same issue on Connexion forum , I received following feedback: spec-first/connexion#2062 (comment)
What I'm looking for is recommendations on better implementation to avoid spike in Memory consumption.
Beta Was this translation helpful? Give feedback.
All reactions