如何清理与send_file一起使用的临时文件? [英] How to clean up temporary file used with send_file?

查看:378
本文介绍了如何清理与send_file一起使用的临时文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个服务器端json接口,其中有几个临时文件在请求期间被操作。



我目前的解决方案是清理这些文件请求看起来像这样:

pre $ $ $ $ $ $ b def api_entry():
以ObjectThatCreatesTemporaryFiles()作为对象:
object.createTemporaryFiles()
返回blabalbal

在这种情况下,清理需要花费在对象中._出口__()



然而在少数情况下,我需要返回一个临时文件到客户端,在这种情况下代码如下所示:

  @ app.route(/ method ,方法= ['POST'])
def api_entry():
with ObjectThatCreatesTemporaryFiles()as object:
object.createTemporaryFiles()
return send_file(object.somePath)

目前这不起作用,因为当我进行了清理,烧瓶正在读取文件并发送给客户端。
¨
我该如何解决这个问题?

编辑:我忘了提及这些文件位于临时目录中。

$如果您使用的是Flask 0.9或更高版本,您可以使用 /#flask.after_this_requestrel =noreferrer> after_this_request 装饰者:

  @app.route(/ method,methods = ['POST'])
def api_entry():
tempcreator = ObjectThatCreatesTemporaryFiles():
tempcreator.createTemporaryFiles ()

@ after_this_request
def cleanup(响应):
tempcreator .__ exit __()
返回响应

返回send_file(tempcreator。 somePath)

编辑

既然不行,你可以尝试使用 cStringIO 代替(th假设你的文件足够小以适应内存):

$ $ $ $ $ $ c $ @ app.route(/ method,methods = [POST])
def api_entry():
file_data = dataObject.createFileData()
#最简单的`createFileData`方法:
#return cStringIO.StringIO \\ bata b)b $ b返回send_file(file_data,
as_attachment = True,
mimetype =text / plain,
attachment_filename =somefile.txt)

或者,您可以像现在一样创建临时文件,但是 取决于您的应用程序删除它们。相反,设置一个cron作业(如果您在Windows上运行,则是一个计划任务),每隔一小时左右运行一次,并删除临时目录中超过半小时之前创建的文件。


I'm currently developing a server side json interface where several temporary files are manipulating during requests.

My current solution for cleaning up these files at the end of the request looks like this:

@app.route("/method",methods=['POST'])
def api_entry():
    with ObjectThatCreatesTemporaryFiles() as object:
        object.createTemporaryFiles()
        return "blabalbal"

In this case, the cleanup takes lace in object.__exit__()

However in a few cases I need to return a temporary files to the client, in which case the code looks like this:

@app.route("/method",methods=['POST'])
def api_entry():
    with ObjectThatCreatesTemporaryFiles() as object:
        object.createTemporaryFiles()
        return send_file(object.somePath)

This currently does not work, because when I the cleanup takes place flask is in the process of reading the file and sending it to the client. ¨ How can I solve this?

Edit: I Forgot to mention that the files are located in temporary directories.

解决方案

If you are using Flask 0.9 or greater you can use the after_this_request decorator:

@app.route("/method",methods=['POST'])
def api_entry():
    tempcreator = ObjectThatCreatesTemporaryFiles():
    tempcreator.createTemporaryFiles()

    @after_this_request
    def cleanup(response):
        tempcreator.__exit__()
        return response

    return send_file(tempcreator.somePath)

EDIT

Since that doesn't work, you could try using cStringIO instead (this assumes that your files are small enough to fit in memory):

@app.route("/method", methods=["POST"])
def api_entry():
    file_data = dataObject.createFileData()
    # Simplest `createFileData` method:  
    # return cStringIO.StringIO("some\ndata")
    return send_file(file_data,
                        as_attachment=True,
                        mimetype="text/plain",
                        attachment_filename="somefile.txt")

Alternately, you could create the temporary files as you do now, but not depend on your application to delete them. Instead, set up a cron job (or a Scheduled Task if you are running on Windows) to run every hour or so and delete files in your temporary directory that were created more than half an hour before.

这篇关于如何清理与send_file一起使用的临时文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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