通过创建蓝图并让它完成所有静态文件的提升来解决它。我会向 Flask 和 Quart 提出建议,以添加此功能的正式版本。如果您使用的是 Flask,而不是 Quart,则将所有async def
s 更改为def
静态bp.py:
from quart import Blueprint, request
import threading
import time
import os
static = Blueprint('static', __name__, static_url_path="/", static_folder="static")
@static.after_request
async def after_request_func(response):
if response.status_code == 200:
file_path = request.base_url.replace("http://ip:port/", "")
t = threading.Thread(target=delete_after_request_thread, args=[file_path])
t.setDaemon(False)
t.start()
return response
def delete_after_request_thread(file_path):
time.sleep(2000)
os.remove(file_path)
main.py(如果您正在运行 Flask,请将 Quart 替换为 Flask):
app = Quart(__name__, "/static", static_folder=None)
app.register_blueprint(static, url_prefix='/static')