Skip to content

Commit 06e78d5

Browse files
committed
simplify unconditional use of tqdm
1 parent 2f52cfc commit 06e78d5

File tree

2 files changed

+18
-28
lines changed

2 files changed

+18
-28
lines changed

ipyparallel/client/asyncresult.py

+17-28
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
from Queue import Queue
1919

2020
from decorator import decorator
21+
import tqdm
2122
import zmq
2223
from zmq import MessageTracker
2324

2425
from IPython import get_ipython
25-
from IPython.core.display import clear_output, display, display_pretty
26+
from IPython.core.display import display, display_pretty
2627
from ipyparallel import error
2728
from ipyparallel.util import utcnow, compare_datetimes, _parse_date
2829
from ipython_genutils.py3compat import string_types
@@ -57,7 +58,8 @@ def check_ready(f, self, *args, **kwargs):
5758
class AsyncResult(Future):
5859
"""Class for representing results of non-blocking calls.
5960
60-
Provides the same interface as :py:class:`multiprocessing.pool.AsyncResult`.
61+
Extends the interfaces of :py:class:`multiprocessing.pool.AsyncResult`
62+
and :py:class:`concurrent.futures.Future`.
6163
"""
6264

6365
msg_ids = None
@@ -73,6 +75,7 @@ def __init__(
7375
fname='unknown',
7476
targets=None,
7577
owner=False,
78+
progress=tqdm.tqdm,
7679
):
7780
super(AsyncResult, self).__init__()
7881
if not isinstance(children, list):
@@ -92,6 +95,7 @@ def __init__(
9295
self._fname = fname
9396
self._targets = targets
9497
self.owner = owner
98+
self.progress = progress
9599

96100
self._ready = False
97101
self._ready_event = Event()
@@ -566,37 +570,22 @@ def wall_time(self):
566570
"""
567571
return self.timedelta(self.submitted, self.received)
568572

569-
def wait_interactive(self, interval=1.0, timeout=-1, progress=None):
570-
"""interactive wait, printing progress at regular intervals.
571-
572-
progress can be a tqdm-like progress bar."""
573-
574-
use_progressbar = progress is not None
573+
def wait_interactive(self, interval=1.0, timeout=-1):
574+
"""interactive wait, printing progress at regular intervals."""
575575
if timeout is None:
576576
timeout = -1
577577
N = len(self)
578-
tic = time.time()
579-
if use_progressbar:
580-
progress_bar = progress(total=N)
581-
n_prev = 0
582-
while not self.ready() and (timeout < 0 or time.time() - tic <= timeout):
578+
tic = time.perf_counter()
579+
progress_bar = self.progress(total=N, unit='tasks', desc=self._fname)
580+
n_prev = 0
581+
while not self.ready() and (
582+
timeout < 0 or time.perf_counter() - tic <= timeout
583+
):
583584
self.wait(interval)
584-
if use_progressbar:
585-
progress_bar.update(self.progress - n_prev)
586-
n_prev = self.progress
587-
else:
588-
clear_output(wait=True)
589-
print(
590-
"%4i/%i tasks finished after %4i s"
591-
% (self.progress, N, self.elapsed),
592-
end="",
593-
)
594-
sys.stdout.flush()
585+
progress_bar.update(self.progress - n_prev)
586+
n_prev = self.progress
595587

596-
if use_progressbar:
597-
progress_bar.close()
598-
else:
599-
print("\ndone")
588+
progress_bar.close()
600589

601590
def _republish_displaypub(self, content, eid):
602591
"""republish individual displaypub content dicts"""

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ def run(self):
123123
"ipykernel>=4.4",
124124
"tornado>=5.1",
125125
"python-dateutil>=2.1",
126+
"tqdm",
126127
],
127128
python_requires=">=3.6",
128129
extras_require={

0 commit comments

Comments
 (0)