Multiprocessing Snippet in Python

Sometimes it's nice to just have a simple function that does unordered multiprocessing in Python with a progress bar. Is that too much to ask? Because I found myself writing this again and again, here it is so that you may copy and paste indiscriminately.

from typing import List, TypeVar, Callable
from tqdm import tqdm
from multiprocessing import Pool

T = TypeVar("T")
U = TypeVar("U")

def pool_map(
    mapper: Callable[[T], U],
    items: List[T],
    cpu_count: int = 4
) -> List[U]:
    """
    Unordered map over results with a progress bar
    
    Args:
        mapper: the function that maps from T -> U
        items: the things we need to proocess
        cpu_count: the number of process in the pool
        
    Returns:
        unordered values of type U
    """
    results: List[U] = []

    with Pool(cpu_count) as pool:
        with tqdm(total=len(items)) as progress_bar:
            apply_map = pool.imap_unordered if cpu_count > 1 else map

            for result in apply_map(mapper, items):
                progress_bar.update(1)
                results.append(result)

    return results

And that's a wrap! Just call it and map over the results however you need.