From f43734c8be50cf01dac8d7e4bfa805419413d5b7 Mon Sep 17 00:00:00 2001 From: Wim Pomp Date: Fri, 13 Sep 2024 14:53:42 +0200 Subject: [PATCH] - fix zeros length iterator bug - fix bug where sometimes not all results are returned --- parfor/__init__.py | 17 ++++++++++------- pyproject.toml | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/parfor/__init__.py b/parfor/__init__.py index 7083674..e251ce5 100644 --- a/parfor/__init__.py +++ b/parfor/__init__.py @@ -145,7 +145,7 @@ class Chunks(Iterable): number = int(cpu_count * ratio) self.iterators = [iter(arg) for arg in iterables] self.number_of_items = length - self.length = max(1, min(length, number)) + self.length = min(length, number) self.lengths = [((i + 1) * self.number_of_items // self.length) - (i * self.number_of_items // self.length) for i in range(self.length)] @@ -674,10 +674,11 @@ def gmap(fun: Callable[[Iteration, Any, ...], Result], iterable: Iterable[Iterat else: if yield_index: for i, c in enumerate(iterable): - yield i, chunk_fun(c, *args, **kwargs)[0] + for q in chunk_fun(c, *args, **kwargs): + yield i, q else: for c in iterable: - yield chunk_fun(c, *args, **kwargs)[0] + yield from chunk_fun(c, *args, **kwargs) else: # parallel case with ExitStack() as stack: @@ -710,18 +711,20 @@ def gmap(fun: Callable[[Iteration, Any, ...], Result], iterable: Iterable[Iterat if yield_ordered: if yield_index: for i in range(len(iterable)): - yield i, p[i][0] + for q in p[i]: + yield i, q else: for i in range(len(iterable)): - yield p[i][0] + yield from p[i] else: if yield_index: for _ in range(len(iterable)): i, n = p.get_newest() - yield i, n[0] + for q in n: + yield i, q else: for _ in range(len(iterable)): - yield p.get_newest()[1][0] + yield from p.get_newest()[1] @wraps(gmap) diff --git a/pyproject.toml b/pyproject.toml index 6019a13..a26b717 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "parfor" -version = "2024.9.1" +version = "2024.9.2" description = "A package to mimic the use of parfor as done in Matlab." authors = ["Wim Pomp "] license = "GPLv3"