[#276] Context manager for parralel func

Signed-off-by: Dmitriy Zayakin <d.zayakin@yadro.com>
This commit is contained in:
Dmitriy Zayakin 2024-08-01 16:32:41 +03:00 committed by Andrey Berezin
parent 6b036a09b7
commit 8306a9f3ff

View file

@ -1,10 +1,22 @@
import itertools import itertools
from concurrent.futures import Future, ThreadPoolExecutor from concurrent.futures import Future, ThreadPoolExecutor
from contextlib import contextmanager
from typing import Callable, Collection, Optional, Union from typing import Callable, Collection, Optional, Union
MAX_WORKERS = 50 MAX_WORKERS = 50
@contextmanager
def parallel_workers_limit(workers_count: int):
global MAX_WORKERS
original_value = MAX_WORKERS
MAX_WORKERS = workers_count
try:
yield
finally:
MAX_WORKERS = original_value
def parallel( def parallel(
fn: Union[Callable, list[Callable]], fn: Union[Callable, list[Callable]],
parallel_items: Optional[Collection] = None, parallel_items: Optional[Collection] = None,