From 7f11b3ad347465efb78e87ef0da2450b1c3d114b Mon Sep 17 00:00:00 2001 From: zy123 <646228430@qq.com> Date: Thu, 12 Dec 2024 13:47:45 +0800 Subject: [PATCH] =?UTF-8?q?12.12=20=E8=B1=86=E5=8C=85=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- flask_app/general/清除file_id.py | 35 ++++++++++++++++++++++---------- flask_app/routes/utils.py | 19 ++++++++++++++++- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/flask_app/general/清除file_id.py b/flask_app/general/清除file_id.py index ce1a4f9..8cdc7b5 100644 --- a/flask_app/general/清除file_id.py +++ b/flask_app/general/清除file_id.py @@ -57,6 +57,8 @@ def read_file_ids(output_folder): return file_ids +from concurrent.futures import ThreadPoolExecutor, as_completed + def delete_file_by_ids(file_ids): """ 根据传入的 file_id 列表删除指定的文件。 @@ -79,17 +81,25 @@ def delete_file_by_ids(file_ids): else: valid_file_ids = file_ids + def delete_file(file_id): + try: + # 假设 openai.File.delete 会返回一个文件对象 + file_object = client.files.delete(file_id) + return None # 成功返回 None + except Exception as e: + print(f"Failed to delete file with id {file_id}: {e}") + return file_id # 返回失败的 file_id + try: - # 删除指定文件 - for file_id in valid_file_ids: - try: - # 假设 openai.File.delete 会返回一个文件对象 - file_object = client.files.delete(file_id) - # print(f"Deleted file with id: {file_id} - {file_object}") - except Exception as e: - # 处理删除单个文件时的异常 - print(f"Failed to delete file with id {file_id}: {e}") - failed_file_ids.append(file_id) + with ThreadPoolExecutor(max_workers=len(valid_file_ids)) as executor: + # 提交所有删除任务 + futures = {executor.submit(delete_file, file_id): file_id for file_id in valid_file_ids} + + # 收集结果 + for future in as_completed(futures): + result = future.result() + if result is not None: # 如果返回值不是 None,表示删除失败 + failed_file_ids.append(result) except Exception as e: print(f"An error occurred while processing the file_ids: {e}") @@ -99,5 +109,8 @@ def delete_file_by_ids(file_ids): return failed_file_ids + if __name__ == '__main__': - delete_all_files() + # delete_all_files() + file_ids=["file-fe-Lu6GKmTR1NjimC2tOebRAIYT"] + delete_file_by_ids(file_ids) \ No newline at end of file diff --git a/flask_app/routes/utils.py b/flask_app/routes/utils.py index a2b3d80..f4a7c33 100644 --- a/flask_app/routes/utils.py +++ b/flask_app/routes/utils.py @@ -4,6 +4,7 @@ from functools import wraps from flask import request, jsonify, current_app, g +from flask_app.general.清除file_id import read_file_ids, delete_file_by_ids from flask_app.logger_setup import create_logger @@ -112,4 +113,20 @@ def validate_and_setup_logger(f): else: # 验证失败,返回错误响应 return validation_result - return decorated_function \ No newline at end of file + return decorated_function + +def perform_cleanup(output_folder, logger): + """ + 清理逻辑:删除临时文件或其他资源。 + """ + try: + if output_folder: + logger.info(f"正在清理输出文件夹: {output_folder}") + file_ids = read_file_ids(output_folder) + failed_file_ids = delete_file_by_ids(file_ids) + if failed_file_ids: + logger.error(f"以下文件删除失败: {failed_file_ids}") + else: + logger.info("清理完毕!") + except Exception as e: + logger.error(f"清理过程中发生异常: {str(e)}") \ No newline at end of file