带大文件的flask make_response [英] flask make_response with large files

查看:545
本文介绍了带大文件的flask make_response的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对文件I / O和内存限制等确实是绿色的,并且我很难让我的Web应用程序成功地将大文件下载提供给烧瓶为 make_response 。以下代码适用于较小的文件(<〜1GB),但是当我进入较大的文件时,会出现 MemoryError 异常:

So I'm real green with file I/O and memory limits and the such, and I'm having a rough time getting my web application to successfully serve large file downloads to a web browser with flask's make_response. The following code works on smaller files (<~1GB), but gives me a MemoryError Exception when I get into larger files:

raw_bytes = ""
with open(file_path, 'rb') as r:
    for line in r:
        raw_bytes = raw_bytes + line
response = make_response(raw_bytes)
response.headers['Content-Type'] = "application/octet-stream"
response.headers['Content-Disposition'] = "inline; filename=" + file_name
return response

我假设粘贴超过2将GB值的二进制数据转换成字符串可能是一个很大的禁忌,但是我不知道替代这些文件下载的方法。如果有人可以用大块的[?]或缓冲的方法来使我正确地进行文件下载,或者只是将我指向一些中级资源以促进对这些内容的更深入了解,我将不胜感激。谢谢!

I'm assuming that sticking over 2 GB worth of binary data into a string is probably a big no-no, but I don't know an alternative to accomplishing these file download black magicks. If someone could get me on the right track with a chunky[?] or buffered approach for file downloads, or just point me toward some intermediate-level resources to facilitate a deeper understanding of this stuff, I would greatly appreciate it. Thanks!

推荐答案

请参阅流内容。基本上,您编写了一个生成数据块的函数,并将该生成器传递给响应,而不是一次传递整个响应。烧瓶和您的Web服务器完成其余工作。

See the docs on Streaming Content. Basically, you write a function that yields chunks of data, and pass that generator to the response, rather than the whole thing at once. Flask and your web server do the rest.

from flask import stream_with_context, Response

@app.route('/stream_data')
def stream_data():
    def generate():
        # create and return your data in small parts here
        for i in xrange(10000):
            yield str(i)

    return Response(stream_with_context(generate()))

如果文件是静态的,则可以利用 send_from_directory() 。文档建议您使用nginx或其他支持X-SendFile的服务器,以便高效地读取和发送数据。

If the file is static, you can instead take advantage of send_from_directory(). The docs advise you to use nginx or another server that supports X-SendFile, so that reading and sending the data is efficient.

这篇关于带大文件的flask make_response的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆