Skip to content

Commit

Permalink
fix: preserve order for mt_loop and mp_loop (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
yxlao authored Mar 14, 2024
1 parent 7506d36 commit 47dbc35
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions camtools/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ def mt_loop(
"""
desc = f"[mt] {func.__name__}"
with ThreadPoolExecutor() as executor:
futures = [executor.submit(func, item, **kwargs) for item in inputs]
progress = tqdm(
as_completed(futures),
total=len(inputs),
desc=desc,
)
results = [future.result() for future in progress]
future_to_index = {
executor.submit(func, item, **kwargs): i for i, item in enumerate(inputs)
}
results = [None] * len(inputs)
for future in tqdm(as_completed(future_to_index), total=len(inputs), desc=desc):
results[future_to_index[future]] = future.result()
return results


Expand All @@ -54,15 +53,12 @@ def mp_loop(
"""
desc = f"[mp] {func.__name__}"
with ProcessPoolExecutor() as executor:
future_to_item = {
executor.submit(func, item, **kwargs): item for item in inputs
future_to_index = {
executor.submit(func, item, **kwargs): i for i, item in enumerate(inputs)
}
progress = tqdm(
as_completed(future_to_item),
total=len(inputs),
desc=desc,
)
results = [future.result() for future in progress]
results = [None] * len(inputs)
for future in tqdm(as_completed(future_to_index), total=len(inputs), desc=desc):
results[future_to_index[future]] = future.result()
return results


Expand Down

0 comments on commit 47dbc35

Please sign in to comment.