from concurrent.futures import ProcessPoolExecutor

import numpy as np
from numpy import ndarray

from type import APV

__all__ = [
    "get_background_noise",
]


def get_background_noise(mapper: APV, num_workers=4) -> ndarray:

    with ProcessPoolExecutor(max_workers=num_workers) as executor:
        results = list(executor.map(np.load, [item["dir"] for item in mapper]))

    array = np.around(np.array(results).mean(axis=0), decimals=1)  # 保留一位小数

    # array = []
    # for item in choice:
    #     if pool is not None:
    #         pool.apply_async(np.load, args=(item["dir"],), callback=array.append)
    #     else:
    #         array.append(np.load(item["dir"]))
    #
    # if pool is not None:
    #     pool.close()  # 必须先close，禁止向池中添加任务，不然join报错
    #     pool.join()  # 没有这两句代码，多进程不会启动
    #
    # array = np.around(np.array(array).mean(axis=0), decimals=1)  # 保留一位小数

    return array

from concurrent.futures import ProcessPoolExecutor

import numpy as np
from numpy import ndarray

from type import APV

__all__ = [
    "get_background_noise",
]


def get_background_noise(mapper: APV, num_workers=4) -> ndarray:

    with ProcessPoolExecutor(max_workers=num_workers) as executor:
        results = list(executor.map(np.load, [item["dir"] for item in mapper]))

    array = np.around(np.array(results).mean(axis=0), decimals=1)  # 保留一位小数

    # array = []
    # for item in choice:
    #     if pool is not None:
    #         pool.apply_async(np.load, args=(item["dir"],), callback=array.append)
    #     else:
    #         array.append(np.load(item["dir"]))
    #
    # if pool is not None:
    #     pool.close()  # 必须先close，禁止向池中添加任务，不然join报错
    #     pool.join()  # 没有这两句代码，多进程不会启动
    #
    # array = np.around(np.array(array).mean(axis=0), decimals=1)  # 保留一位小数

    return array

# 保留一位小数 # array = [] # for item in choice: #     if pool is not None: #         pool.apply_async(np.load, args=(item["dir"],), callback=array.append) #     else: #         array.append(np.load(item["dir"])) # # if pool is not None: #     pool.close()  # 必须先close，禁止向池中添加任务，不然join报错 #     pool.join()  # 没有这两句代码，多进程不会启动 # # array = np.around(np.array(array).mean(axis=0), decimals=1)  # 保留一位小数