[Python] FastAPI UploadFile is slow compared to Flask

Discussão em 'Python' iniciado por Stack, Janeiro 17, 2025.

  1. Stack

    Stack Membro Participativo

    I have created a FastAPI endpoint, as shown below:

    @app.post("/report/upload")
    def create_upload_files(files: UploadFile = File(...)):
    try:
    with open(files.filename,'wb+') as wf:
    wf.write(file.file.read())
    wf.close()
    except Exception as e:
    return {"error": e.__str__()}


    It is launched with uvicorn:

    ../venv/bin/uvicorn test_upload:app --host=0.0.0.0 --port=5000 --reload


    I am performing some tests of uploading a file of around 100 MB using Python requests, and that takes around 128 seconds to complete:

    f = open(sys.argv[1],"rb").read()
    hex_convert = binascii.hexlify(f)
    items = {"files": hex_convert.decode()}
    start = time.time()
    r = requests.post("http://192.168.0.90:5000/report/upload",files=items)
    end = time.time() - start
    print(end)


    I tested the same upload script with an API endpoint using Flask and takes around 0.5 seconds:

    from flask import Flask, render_template, request
    app = Flask(__name__)


    @app.route('/uploader', methods = ['GET', 'POST'])
    def upload_file():
    if request.method == 'POST':
    f = request.files['file']
    f.save(f.filename)
    return 'file uploaded successfully'

    if __name__ == '__main__':
    app.run(host="192.168.0.90",port=9000)


    Is there anything I am doing wrong?

    Continue reading...

Compartilhe esta Página