Skip to content

Commit 4135d83

Browse files
authored
Merge pull request #47 from r-menezes/main
use `with ... as ... ` syntax to instantiate Pool
2 parents 063c3fd + 48b0940 commit 4135d83

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

p_tqdm/p_tqdm.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,17 @@ def _parallel(ordered: bool, function: Callable, *iterables: Iterable, **kwargs:
4545

4646
# Create parallel generator
4747
map_type = 'imap' if ordered else 'uimap'
48-
pool = Pool(num_cpus)
49-
map_func = getattr(pool, map_type)
5048

5149
# Choose tqdm variant
5250
tqdm_func = kwargs.pop('tqdm', tqdm)
51+
52+
with Pool(num_cpus) as pool:
53+
map_func = getattr(pool, map_type)
5354

54-
for item in tqdm_func(map_func(function, *iterables), total=length, **kwargs):
55-
yield item
55+
for item in tqdm_func(map_func(function, *iterables), total=length, **kwargs):
56+
yield item
5657

57-
pool.clear()
58+
pool.clear()
5859

5960

6061
def p_map(function: Callable, *iterables: Iterable, **kwargs: Any) -> List[Any]:

0 commit comments

Comments
 (0)