from concurrent.futures import ProcessPoolExecutor, as_completed

from framework.env_config import get_config

config = get_config(__name__)
process_pool = ProcessPoolExecutor(max_workers=config['max-workers'] or 2)


def create_process_pool(max_workers=None):
    return ProcessPoolExecutor(max_workers=max_workers or config['max-workers'])


def block_execute(func, params: dict, isolate=False) -> dict:
    if isolate:
        with create_process_pool() as process:
            futures = {process.submit(func, *x[1]): x[0] for x in params.items()}
            return {futures[x]: x.result() for x in as_completed(futures)}
    else:
        futures = {process_pool.submit(func, *x[1]): x[0] for x in params.items()}
        return {futures[x]: x.result() for x in as_completed(futures)}