栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从Flask下载文件后将其删除

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从Flask下载文件后将其删除

有几种方法可以做到这一点。

send_file
然后立即删除(仅限Linux)
Flask有一个
after_this_request
装饰器,可用于此用例:

@app.route('/files/<filename>/download')def download_file(filename):    file_path = derive_filepath_from_filename(filename)    file_handle = open(file_path, 'r')    @after_this_request    def remove_file(response):        try: os.remove(file_path) file_handle.close()        except Exception as error: app.logger.error("Error removing or closing downloaded file handle", error)        return response    return send_file(file_handle)

问题是,这仅在Linux上有效(即使在删除文件后仍存在指向其的打开文件指针,该文件也可以读取)。它也不会一直有效(我听说有报道说,

send_file
在Flask已经取消链接文件之前,有时不会结束对内核的调用)。它并不会阻塞Python进程来发送文件。

流文件,然后删除
理想情况下,尽管你在知道操作系统已将其流式传输到客户端后清理了文件。你可以通过以下方式来实现此目的:创建一个生成器,将生成的文件流式传输,然后将其关闭,然后通过Python将其流式传输回Python,如以下答案所示:

def download_file(filename):    file_path = derive_filepath_from_filename(filename)    file_handle = open(file_path, 'r')    # This *replaces* the `remove_file` + @after_this_request pre above    def stream_and_remove_file():        yield from file_handle        file_handle.close()        os.remove(file_path)    return current_app.response_class(        stream_and_remove_file(),        headers={'Content-Disposition': 'attachment', 'filename': filename}    )

这种方法很好,因为它是跨平台的。但是,这并不是灵丹妙药,因为它束缚了Python Web进程,直到将整个文件传输到客户端为止。

清理计时器
在计时器上运行另一个进程(

cron
也许使用),或使用进程调度程序(例如APScheduler)并清理超出超时(例如,半小时,一周,三十天,之后它们在RDMBS中被标记为“已下载”)

这是最可靠的方法,但是需要额外的复杂性(cron,进程内调度程序,工作队列等)。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/381124.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号