# test_内存泄漏.py
from memory_profiler import memory_usage
import time
from flask_app.general.读取文件.按页读取pdf import read_pdf_main
from flask_app.general.通用功能函数 import get_global_logger
from flask_app.routes.货物标解析main import preprocess_file_main

def process_data():
    # 模拟一些占用内存的操作
    data = [i for i in range(1000000)]  # 建一个1000万元素的list
    time.sleep(1)  # 假装处理2秒
    return len(data)

def call():
    a = 1
    process_data()
    return a

def process_pdf(pdf_path):
    """子进程里实际执行的函数"""
    result = read_pdf_main(pdf_path=pdf_path)
    return result

def main():
    for i in range(3):
        logger = get_global_logger("123")
        pdf_path1=r'C:\Users\Administrator\Desktop\工程标\招标test文件夹\zbtest13.pdf'
        pdf_path2 = r'C:\Users\Administrator\Desktop\fsdownload\4fb2f541-29c3-497f-9f0a-5216a10591a1\tmp\ztbfile.pdf'
        pdf_path3=  r'C:\Users\Administrator\Desktop\fsdownload\2c3e2291-6804-4ef0-b4a8-6f457edd5709\tmp\ztbfile.pdf'
        output_folder1 = r'C:\Users\Administrator\Desktop\fsdownload\4fb2f541-29c3-497f-9f0a-5216a10591a1\tmp'
        output_folder2 = r'C:\Users\Administrator\Desktop\fsdownload\4fb2f541-29c3-497f-9f0a-5216a10591a1\tmp'
        output_folder3=r'C:\Users\Administrator\Desktop\fsdownload\2c3e2291-6804-4ef0-b4a8-6f457edd5709\tmp'
        mem_before = memory_usage()[0]
        if i % 3 == 0:
            output_folder = output_folder1
            pdf_path = pdf_path1
        elif i %3 ==1:
            output_folder = output_folder2
            pdf_path = pdf_path2
        else:
            output_folder = output_folder3
            pdf_path = pdf_path3
        result=preprocess_file_main(output_folder, pdf_path, 2,logger)
        process_pdf(pdf_path)
        response_data = {
            'result': {
                'procurement_spec': result['procurement_path'],
                'evaluation_method': result['evaluation_method_path'],
                'qualification_docs': result['qualification_path'],
                'notice_docs': result['notice_path'],
                'clause_details': result['clause_path'],
                'merged_baseinfo': result['merged_baseinfo_path']
            }
        }
        print(response_data)
        mem_after = memory_usage()[0]
        print(f"Memory before: {mem_before} MiB, Memory after: {mem_after} MiB")


if __name__ == '__main__':
    main()
