• <legend id='ThZ6h'><style id='ThZ6h'><dir id='ThZ6h'><q id='ThZ6h'></q></dir></style></legend>
    <tfoot id='ThZ6h'></tfoot>

    <i id='ThZ6h'><tr id='ThZ6h'><dt id='ThZ6h'><q id='ThZ6h'><span id='ThZ6h'><b id='ThZ6h'><form id='ThZ6h'><ins id='ThZ6h'></ins><ul id='ThZ6h'></ul><sub id='ThZ6h'></sub></form><legend id='ThZ6h'></legend><bdo id='ThZ6h'><pre id='ThZ6h'><center id='ThZ6h'></center></pre></bdo></b><th id='ThZ6h'></th></span></q></dt></tr></i><div id='ThZ6h'><tfoot id='ThZ6h'></tfoot><dl id='ThZ6h'><fieldset id='ThZ6h'></fieldset></dl></div>

      <bdo id='ThZ6h'></bdo><ul id='ThZ6h'></ul>

    <small id='ThZ6h'></small><noframes id='ThZ6h'>

        FastAPI UploadFile慢速

        时间:2024-08-11
        • <bdo id='vo0k5'></bdo><ul id='vo0k5'></ul>
          <legend id='vo0k5'><style id='vo0k5'><dir id='vo0k5'><q id='vo0k5'></q></dir></style></legend><tfoot id='vo0k5'></tfoot>

            1. <small id='vo0k5'></small><noframes id='vo0k5'>

                    <tbody id='vo0k5'></tbody>
                  <i id='vo0k5'><tr id='vo0k5'><dt id='vo0k5'><q id='vo0k5'><span id='vo0k5'><b id='vo0k5'><form id='vo0k5'><ins id='vo0k5'></ins><ul id='vo0k5'></ul><sub id='vo0k5'></sub></form><legend id='vo0k5'></legend><bdo id='vo0k5'><pre id='vo0k5'><center id='vo0k5'></center></pre></bdo></b><th id='vo0k5'></th></span></q></dt></tr></i><div id='vo0k5'><tfoot id='vo0k5'></tfoot><dl id='vo0k5'><fieldset id='vo0k5'></fieldset></dl></div>
                  本文介绍了FastAPI UploadFile慢速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  您好,我对FastAPI上载文件速度有问题。

                  如果我如下所示创建和结束:

                  @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__()}
                  

                  使用uvicorn:

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

                  我正在做一些测试,根据请求上载一个大约100M的文件,大约需要128秒。

                  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)
                  

                  我测试了在flask中编写API端点的相同上传脚本,花费了大约0.5秒

                  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)
                  

                  我是否做错了什么?

                  感谢您的帮助

                  推荐答案

                  终结点应为异步,并且由于所有UploadFilemethods are async methods, you need to "await" them。因此,文件内容应读作:contents = await myfile.read()

                  您可以使用同步写入方式写入文件,如下所示:

                  @app.post("/upload")
                  async def upload(files: List[UploadFile] = File(...)):
                      for file in files:
                          with open(file.filename, 'wb') as f:
                              contents = await file.read()
                              f.write(contents)
                              
                      return {"Uploaded Filenames": [file.filename for file in files]}
                  

                  或(更好)使用aiofiles的异步写入:

                  @app.post("/upload")
                  async def upload(files: List[UploadFile] = File(...)):
                      for file in files:
                          async with aiofiles.open(file.filename, 'wb') as f:
                              contents = await file.read()
                              await f.write(contents)
                              
                      return {"Uploaded Filenames": [file.filename for file in files]}
                  

                  或异步in the chunked manner, to avoid loading the entire file into memory。不过,此操作需要更长的时间才能完成(取决于您选择的区块大小)。

                  @app.post("/upload")
                  async def upload(files: List[UploadFile] = File(...)):
                      for file in files:
                          async with aiofiles.open(file.filename, 'wb') as f:
                              while content := await file.read(1024): # async read chunk
                                  await f.write(content)
                              
                      return {"Uploaded Filenames": [file.filename for file in files]}
                  

                  通过Python请求测试代码:

                  url = 'http://127.0.0.1:8000/upload'
                  files = [('files', open('test_files/a.txt', 'rb')), ('files', open('test_files/b.txt', 'rb'))]
                  resp = requests.post(url=url, files = files) 
                  print(resp.json())
                  

                  这篇关于FastAPI UploadFile慢速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python:二进制到十进制的转换 下一篇:Pycuda块和格网,用于处理大数据

                  相关文章

                      <bdo id='rNCn6'></bdo><ul id='rNCn6'></ul>
                      <legend id='rNCn6'><style id='rNCn6'><dir id='rNCn6'><q id='rNCn6'></q></dir></style></legend>
                      <tfoot id='rNCn6'></tfoot>

                      <small id='rNCn6'></small><noframes id='rNCn6'>

                      <i id='rNCn6'><tr id='rNCn6'><dt id='rNCn6'><q id='rNCn6'><span id='rNCn6'><b id='rNCn6'><form id='rNCn6'><ins id='rNCn6'></ins><ul id='rNCn6'></ul><sub id='rNCn6'></sub></form><legend id='rNCn6'></legend><bdo id='rNCn6'><pre id='rNCn6'><center id='rNCn6'></center></pre></bdo></b><th id='rNCn6'></th></span></q></dt></tr></i><div id='rNCn6'><tfoot id='rNCn6'></tfoot><dl id='rNCn6'><fieldset id='rNCn6'></fieldset></dl></div>