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

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

问题描述

所以我对文件 I/O 和内存限制等很满意,我很难让我的 Web 应用程序成功地使用 Flask 的 make_response<将大文件下载到 Web 浏览器/代码>.以下代码适用于较小的文件(<~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!

推荐答案

查看 Streaming 上的文档内容.基本上,您编写一个函数来生成数据块,并将该生成器传递给响应,而不是一次将整个事件传递给响应.Flask 和您的网络服务器完成剩下的工作.

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.

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

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