解决内存泄露问题
This commit is contained in:
		
							parent
							
								
									8c840f3d4c
								
							
						
					
					
						commit
						55d807c116
					
				@ -418,6 +418,7 @@ def 编译Latex(chatbot, history, main_file_original, main_file_modified, work_f
 | 
				
			|||||||
                    merge_pdfs(origin_pdf, result_pdf, concat_pdf)
 | 
					                    merge_pdfs(origin_pdf, result_pdf, concat_pdf)
 | 
				
			||||||
                    promote_file_to_downloadzone(concat_pdf, rename_file=None, chatbot=chatbot)  # promote file to web UI
 | 
					                    promote_file_to_downloadzone(concat_pdf, rename_file=None, chatbot=chatbot)  # promote file to web UI
 | 
				
			||||||
                except Exception as e:
 | 
					                except Exception as e:
 | 
				
			||||||
 | 
					                    print(e)
 | 
				
			||||||
                    pass
 | 
					                    pass
 | 
				
			||||||
            return True # 成功啦
 | 
					            return True # 成功啦
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
 | 
				
			|||||||
@ -493,11 +493,38 @@ def compile_latex_with_timeout(command, cwd, timeout=60):
 | 
				
			|||||||
        return False
 | 
					        return False
 | 
				
			||||||
    return True
 | 
					    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):
 | 
					def _merge_pdfs(pdf1_path, pdf2_path, output_path):
 | 
				
			||||||
    import PyPDF2
 | 
					    import PyPDF2   # PyPDF2这个库有严重的内存泄露问题,把它放到子进程中运行,从而方便内存的释放
 | 
				
			||||||
    Percent = 0.95
 | 
					    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
 | 
					    # Open the first PDF file
 | 
				
			||||||
    with open(pdf1_path, 'rb') as pdf1_file:
 | 
					    with open(pdf1_path, 'rb') as pdf1_file:
 | 
				
			||||||
        pdf1_reader = PyPDF2.PdfFileReader(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
 | 
					            # Save the merged PDF file
 | 
				
			||||||
            with open(output_path, 'wb') as output_file:
 | 
					            with open(output_path, 'wb') as output_file:
 | 
				
			||||||
                output_writer.write(output_file)
 | 
					                output_writer.write(output_file)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					merge_pdfs = run_in_subprocess(_merge_pdfs) # PyPDF2这个库有严重的内存泄露问题,把它放到子进程中运行,从而方便内存的释放
 | 
				
			||||||
 | 
				
			|||||||
@ -561,7 +561,8 @@ def promote_file_to_downloadzone(file, rename_file=None, chatbot=None):
 | 
				
			|||||||
        user_name = get_user(chatbot)
 | 
					        user_name = get_user(chatbot)
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        user_name = default_user_name
 | 
					        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)
 | 
					    user_path = get_log_folder(user_name, plugin_name=None)
 | 
				
			||||||
    if file_already_in_downloadzone(file, user_path):
 | 
					    if file_already_in_downloadzone(file, user_path):
 | 
				
			||||||
        new_path = file
 | 
					        new_path = file
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user