File size: 947 Bytes
59e40e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""
Source url: https://github.com/OPHoperHPO/image-background-remove-tool
Author: Nikita Selin (OPHoperHPO)[https://github.com/OPHoperHPO].
License: Apache License 2.0
"""
from concurrent.futures import ThreadPoolExecutor
from typing import Any, Iterable


def thread_pool_processing(func: Any, data: Iterable, workers=18):
    """
    Passes all iterator data through the given function

    Args:
        workers: Count of workers.
        func: function to pass data through
        data: input iterator

    Returns:
        function return list

    """
    with ThreadPoolExecutor(workers) as p:
        return list(p.map(func, data))


def batch_generator(iterable, n=1):
    """
    Splits any iterable into n-size packets

    Args:
        iterable: iterator
        n: size of packets

    Returns:
        new n-size packet
    """
    it = len(iterable)
    for ndx in range(0, it, n):
        yield iterable[ndx : min(ndx + n, it)]