title: apache/flask - when uploading file, requests are beeing qued, server unresponsive & memory gets high
body: <p>I'm running Flask on Apache with mod_wsgi, every time a file get uploaded, the response time of the server worses, and my memory goes over 100% to swap.</p>
  
  <p>This below is the whole code that handles my upload</p>
  
  <pre><code>@app.route(sApiRoute + '/f/upload', methods=['POST'])
  @hasToken
  def pbFileUpload(token=None,**kwargs):
      def log_time(start_dt=None,start_time=None,doc=None):
         end_time = None
         if start_time is not None:
            end_time = str(time.time()-start_time)
  
         if end_time is not None and end_time &gt; str(0.3):
            pblog.warning("Upload with long response time. Start: "+str(start_dt)+ "Duration: "+str(end_time)+" \nUploader: " + token["id_user"] + " \nDoc info: " + str(doc))
  
      import time
      start_time = time.time()
      start_dt = current_datetime()
  
  
      # parse path
      path = getUploadPath(request.json, None, token)
      if not path:
          return r404()
      # Get the name of the uploaded file
      filename = request.json["name"]
      filename =     filename.replace("$c$","č").replace("$c2$","ć").replace("$z$","ž").replace("$s$","š").replace("$d$","đ").replace("$C$","Č").replace("$C2$","Ć").replace("$Z$","Ž").replace("$S$","Š").replace("$D$","Đ")
      rawfile = request.json["data"]
      header, data = rawfile.split(',')
      # convert data
      import base64
      recoveredfile = base64.decodebytes(bytes(data, 'UTF-8'))
      # write data
      import uuid
      # leave file name as is
      if request.json.get("leaveName"):
          generatedfilename = filename
          leaveName = True
      else:
          generatedfilename = str(uuid.uuid1())
          leaveName = False
      # use of ftp to save files
      if 'ftp.' in path.lower():
          putFileToFtp(path, filename, generatedfilename, recoveredfile)
  
      # classic directory structure
      else:
          if not os.path.exists(path):
              try:
                  os.mkdir(path)
              except Exception as e:
                  pblog.warning(str(e),exc_info=1)
                  return jsonify(data={"status":False,"msg":"Path not found"})
  
          while os.path.exists(os.path.join(path, generatedfilename)) and not     leaveName:
              generatedfilename = str(uuid.uuid1())
          with open(os.path.join(path, generatedfilename), "wb") as out_file:
              out_file.write(recoveredfile)
          out_file.close()
  
      # send to document system
      if request.json.get("dataRec"):
          db = DBStore.getDB(token["current_company_id"])
          dataRec = request.json.get("dataRec")
          dataRec["idext"] = generatedfilename
          rtd = DocumentCls.getIdByIdent(dataRec["IdDocumentType"],db)
          if rtd and rtd!=[]:
              if rtd.get("Id"):
                  dataRec["IdDocumentType"] = rtd["Id"]
          r = DocumentCls.saveDocument(dataRec,request.json.get("username"),db)
          log_time(start_dt,start_time,dataRec)
          return jsonify(data={"version":     r,"generatedfilename":generatedfilename},**kwargs)
      else:
          log_time(start_dt,start_time,generatedfilename)
          return jsonify(data={"filename": filename, "generatedfilename":     generatedfilename},**kwargs)
  </code></pre>