NamedTemporaryFile() 用于在 flask send_file 之后清除文件
NamedTemporaryFile() for clearing files after flask send_file
我正在使用烧瓶服务器 return 在请求后处理图像文件。我正在使用 google 云 运行 并且根据我对并发性的理解,我应该删除文件以减少旋转实例的内存使用。为此,我选择在 'with' 语句中使用 NamedTemporaryFile() - 我的理解是默认行为是一旦退出 'with' 语句,临时文件就会被删除。
这是我必须要 return 处理过的图像的代码。
@app.route('/')
def hello():
"image processing"
with NamedTemporaryFile() as temp:
cv2.imwrite(str(temp.name),img_processed)
return send_file(str(temp.name), mimetype='image/png')
但是,由于我试图return 'with' 语句中的函数,临时文件是否仍会被删除?
我还正确使用 NamedTemporaryFile() 吗?我看到的示例使用 temp.write 但由于我使用 cv2.imwrite 使用 temp.name 是我想出的方法。
flask send_file 之后有没有更好的删除文件的方法? - 我读过有关使用 @after_this_request 的内容,但 Delete an uploaded file after downloading it from Flask 说它可能不一致。
更新:要将 cv2.imwrite 与 NamedTemporaryFile() 一起使用,我必须使用以下方法指定临时文件的扩展名:
NamedTemporaryFile(suffix='.png')
否则代码工作正常。
However, since I am trying to return the function in the 'with'
statement, will the temp file still be deleted?
我认为是,根据https://docs.python.org/3.9/library/tempfile.html#tempfile.TemporaryFile:
The file is created securely, using the same rules as mkstemp(). It
will be destroyed as soon as it is closed (including an implicit close
when the object is garbage collected)
return 语句将垃圾收集 temp
变量和对象。
The examples I've seen use temp.write but since I am using cv2.imwrite
using temp.name was the method I came up with.
如果你写一个编码字符串,你可以使用write方法:
from tempfile import NamedTemporaryFile
def temp_fx():
with NamedTemporaryFile() as temp:
print(temp.name)
temp.write('abc'.encode())
return 1
temp_fx()
根据 this answer 和 运行 快速测试,这按预期工作。
测试:
In [13]: from pathlib import Path
In [14]: from tempfile import NamedTemporaryFile
In [15]: def deltest(delete=True):
...: with NamedTemporaryFile(delete=delete) as temp:
...: return temp.name
In [16]: file = deltest()
In [17]: Path(file).exists()
Out[17]: False
In [18]: file = deltest(delete=False)
In [19]: Path(file).exists()
Out[19]: True
我正在使用烧瓶服务器 return 在请求后处理图像文件。我正在使用 google 云 运行 并且根据我对并发性的理解,我应该删除文件以减少旋转实例的内存使用。为此,我选择在 'with' 语句中使用 NamedTemporaryFile() - 我的理解是默认行为是一旦退出 'with' 语句,临时文件就会被删除。
这是我必须要 return 处理过的图像的代码。
@app.route('/')
def hello():
"image processing"
with NamedTemporaryFile() as temp:
cv2.imwrite(str(temp.name),img_processed)
return send_file(str(temp.name), mimetype='image/png')
但是,由于我试图return 'with' 语句中的函数,临时文件是否仍会被删除? 我还正确使用 NamedTemporaryFile() 吗?我看到的示例使用 temp.write 但由于我使用 cv2.imwrite 使用 temp.name 是我想出的方法。 flask send_file 之后有没有更好的删除文件的方法? - 我读过有关使用 @after_this_request 的内容,但 Delete an uploaded file after downloading it from Flask 说它可能不一致。
更新:要将 cv2.imwrite 与 NamedTemporaryFile() 一起使用,我必须使用以下方法指定临时文件的扩展名:
NamedTemporaryFile(suffix='.png')
否则代码工作正常。
However, since I am trying to return the function in the 'with' statement, will the temp file still be deleted?
我认为是,根据https://docs.python.org/3.9/library/tempfile.html#tempfile.TemporaryFile:
The file is created securely, using the same rules as mkstemp(). It will be destroyed as soon as it is closed (including an implicit close when the object is garbage collected)
return 语句将垃圾收集 temp
变量和对象。
The examples I've seen use temp.write but since I am using cv2.imwrite using temp.name was the method I came up with.
如果你写一个编码字符串,你可以使用write方法:
from tempfile import NamedTemporaryFile
def temp_fx():
with NamedTemporaryFile() as temp:
print(temp.name)
temp.write('abc'.encode())
return 1
temp_fx()
根据 this answer 和 运行 快速测试,这按预期工作。
测试:
In [13]: from pathlib import Path
In [14]: from tempfile import NamedTemporaryFile
In [15]: def deltest(delete=True):
...: with NamedTemporaryFile(delete=delete) as temp:
...: return temp.name
In [16]: file = deltest()
In [17]: Path(file).exists()
Out[17]: False
In [18]: file = deltest(delete=False)
In [19]: Path(file).exists()
Out[19]: True