diff --git a/crazy_functions/Latex输出PDF结果.py b/crazy_functions/Latex输出PDF结果.py index f3919ed..18a8d1b 100644 --- a/crazy_functions/Latex输出PDF结果.py +++ b/crazy_functions/Latex输出PDF结果.py @@ -88,6 +88,9 @@ def arxiv_download(chatbot, history, txt, allow_cache=True): target_file = pj(translation_dir, 'translate_zh.pdf') if os.path.exists(target_file): promote_file_to_downloadzone(target_file, rename_file=None, chatbot=chatbot) + target_file_compare = pj(translation_dir, 'comparison.pdf') + if os.path.exists(target_file_compare): + promote_file_to_downloadzone(target_file_compare, rename_file=None, chatbot=chatbot) return target_file return False def is_float(s): diff --git a/crazy_functions/latex_fns/latex_actions.py b/crazy_functions/latex_fns/latex_actions.py index 74e8757..be3d52e 100644 --- a/crazy_functions/latex_fns/latex_actions.py +++ b/crazy_functions/latex_fns/latex_actions.py @@ -418,6 +418,7 @@ def 编译Latex(chatbot, history, main_file_original, main_file_modified, work_f merge_pdfs(origin_pdf, result_pdf, concat_pdf) promote_file_to_downloadzone(concat_pdf, rename_file=None, chatbot=chatbot) # promote file to web UI except Exception as e: + print(e) pass return True # 成功啦 else: diff --git a/crazy_functions/latex_fns/latex_toolbox.py b/crazy_functions/latex_fns/latex_toolbox.py index 4555ff1..0a6a873 100644 --- a/crazy_functions/latex_fns/latex_toolbox.py +++ b/crazy_functions/latex_fns/latex_toolbox.py @@ -493,11 +493,38 @@ def compile_latex_with_timeout(command, cwd, timeout=60): return False return True +def run_in_subprocess_wrapper_func(func, args, kwargs, return_dict, exception_dict): + import sys + try: + result = func(*args, **kwargs) + return_dict['result'] = result + except Exception as e: + exc_info = sys.exc_info() + exception_dict['exception'] = exc_info +def run_in_subprocess(func): + import multiprocessing + def wrapper(*args, **kwargs): + return_dict = multiprocessing.Manager().dict() + exception_dict = multiprocessing.Manager().dict() + process = multiprocessing.Process(target=run_in_subprocess_wrapper_func, + args=(func, args, kwargs, return_dict, exception_dict)) + process.start() + process.join() + process.close() + if 'exception' in exception_dict: + # ooops, the subprocess ran into an exception + exc_info = exception_dict['exception'] + raise exc_info[1].with_traceback(exc_info[2]) + if 'result' in return_dict.keys(): + # If the subprocess ran successfully, return the result + return return_dict['result'] + return wrapper -def merge_pdfs(pdf1_path, pdf2_path, output_path): - import PyPDF2 +def _merge_pdfs(pdf1_path, pdf2_path, output_path): + import PyPDF2 # PyPDF2这个库有严重的内存泄露问题,把它放到子进程中运行,从而方便内存的释放 Percent = 0.95 + # raise RuntimeError('PyPDF2 has a serious memory leak problem, please use other tools to merge PDF files.') # Open the first PDF file with open(pdf1_path, 'rb') as pdf1_file: pdf1_reader = PyPDF2.PdfFileReader(pdf1_file) @@ -531,3 +558,5 @@ def merge_pdfs(pdf1_path, pdf2_path, output_path): # Save the merged PDF file with open(output_path, 'wb') as output_file: output_writer.write(output_file) + +merge_pdfs = run_in_subprocess(_merge_pdfs) # PyPDF2这个库有严重的内存泄露问题,把它放到子进程中运行,从而方便内存的释放 diff --git a/toolbox.py b/toolbox.py index 411c29b..f830a3d 100644 --- a/toolbox.py +++ b/toolbox.py @@ -563,7 +563,8 @@ def promote_file_to_downloadzone(file, rename_file=None, chatbot=None): user_name = get_user(chatbot) else: user_name = default_user_name - + if not os.path.exists(file): + raise FileNotFoundError(f'文件{file}不存在') user_path = get_log_folder(user_name, plugin_name=None) if file_already_in_downloadzone(file, user_path): new_path = file