简体   繁体   English

使用python flask上传大型csv文件的最佳方法

[英]Best way to upload large csv files using python flask

Requirement : To Upload files using flask framework. 要求 :使用flask框架上载文件。 Once uploaded to the server user should be able to see the file in UI. 一旦上载到服务器,用户应该能够在UI中看到文件。

Current code : In order to meet above requirement i wrote the code to upload sufficiently large files and its working fine with (~30 MB file, yes ofcourse not that fast). 当前代码 :为了满足上述要求,我编写了代码以上传足够大的文件,并且可以正常工作(〜30 MB文件,当然不是那么快)。 But when i am trying to upload (~100 MB) file, It is taking too long and process never completes. 但是,当我尝试上传(〜100 MB)文件时,它花费的时间太长,并且过程从未完成。

This is what currently i am doing: 这是我目前正在做的事情:

UPLOAD_FOLDER = '/tmp' UPLOAD_FOLDER ='/ tmp'

    file = request.files['filename']
    description = request.form['desc']

    filename = secure_filename(file.filename)
    try:
        file.save(os.path.join(UPLOAD_FOLDER, filename))
        filepath = os.path.join(UPLOAD_FOLDER, filename)
    except Exception as e:
        return e
    data = None
    try:
        with open(filepath) as file:
            data = file.read()
    except Exception as e:
        log.exception(e)

So what i am doing is first saving the file to temporary location in server and then from then reading the data and putting it into our database. 因此,我要做的是先将文件保存到服务器中的临时位置,然后再读取数据并将其放入我们的数据库中。 I think this is where i am struggling i am not sure what is the best approach. 我认为这是我在努力的方向,我不确定什么是最好的方法。

Should i take the input from user and return the success message( obviously user won't be able to access the file immediately then ) and make putting the data into database a background process, using some kind of queue system. 我是否应该接受用户的输入并返回成功消息( 显然用户将无法立即访问文件 ), 然后使用某种队列系统将数据放入数据库中成为后台进程。 Or What else should be done to optimize the code. 或应该做些什么来优化代码。

On the flask side make sure you have the MAX_CONTENT_LENGTH config value set high enough: 在烧瓶一侧,请确保已将MAX_CONTENT_LENGTH配置值设置得足够高:

app.config['MAX_CONTENT_LENGTH'] = 100 * 1024 * 1024  # 100MB limit

Also you may want to look into the Flask-Upload extension . 另外,您可能需要研究Flask-Upload扩展

There is another SO post similar to this one: Large file upload in Flask . 还有另一则与此类似的帖子: Flask中的大文件上传

Other than that you problem may be a timeouts somewhere along the line. 除此之外,您可能遇到的问题可能是超时。 What does the rest of your stack look like? 其余堆栈看起来如何? Apache? 阿帕奇? Nginx and Gunicorn? Nginx和Gunicorn? Are you getting a Connection reset error, Connection timed out error or does it just hang? 您收到Connection reset错误, Connection timed out错误还是只是挂起?

If you are using Nginx try setting proxy_read_timeout to a value high enough for the upload to finish. 如果您使用的是Nginx,请尝试将proxy_read_timeout设置为足够高的值以完成上传。 Apache may also have a default setting causing you trouble if that is what you are using. 如果使用的是Apache,那么默认设置也可能会给您带来麻烦。 It's hard to tell without knowing more about your stack and what the error is that you are getting and what the logs are showing. 如果不了解更多有关堆栈以及您得到的错误是什么以及日志显示的内容,很难说出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM