Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add maximum_refresh_time to run_tasks() #151

Merged
merged 3 commits into from
Jul 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions audeer/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import uuid
import warnings

from audeer.core import tqdm
from audeer.core.tqdm import progress_bar as audeer_progress_bar
from audeer.core.version import LooseVersion


Expand Down Expand Up @@ -541,6 +541,7 @@ def run_tasks(
multiprocessing: bool = False,
progress_bar: bool = False,
task_description: str = None,
maximum_refresh_time: float = None,
) -> typing.List[typing.Any]:
r"""Run parallel tasks using multprocessing.

Expand All @@ -562,6 +563,11 @@ def run_tasks(
progress_bar: show a progress bar
task_description: task description
that will be displayed next to progress bar
maximum_refresh_time: refresh the progress bar
at least every ``maximum_refresh_time`` seconds,
using another thread.
If ``None``,
no refreshing is enforced

Returns:
list of computed results
Expand All @@ -577,10 +583,11 @@ def run_tasks(
results = [None] * num_tasks

if num_workers == 1: # sequential
with tqdm.progress_bar(
with audeer_progress_bar(
params,
total=len(params),
desc=task_description,
maximum_refresh_time=maximum_refresh_time,
disable=not progress_bar,
) as pbar:
for index, param in enumerate(pbar):
Expand All @@ -592,9 +599,10 @@ def run_tasks(
else:
executor = concurrent.futures.ThreadPoolExecutor
with executor(max_workers=num_workers) as pool:
with tqdm.progress_bar(
with audeer_progress_bar(
total=len(params),
desc=task_description,
maximum_refresh_time=maximum_refresh_time,
disable=not progress_bar,
) as pbar:
futures = []
Expand Down Expand Up @@ -668,7 +676,7 @@ def run_worker_threads(
class QueueWithProgbar(queue.Queue):
def __init__(self, num_tasks, maxsize=0):
super().__init__(maxsize)
self.pbar = tqdm.progress_bar(
self.pbar = audeer_progress_bar(
total=num_tasks,
desc=task_description,
)
Expand Down
Loading