Skip to content

Commit 854ff2e

Browse files
authored
Merge pull request #264 from cphyc/master
Support for progress bar when found
2 parents cc3a528 + 37f1722 commit 854ff2e

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

ipyparallel/client/asyncresult.py

+17-12
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_bar=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_bar = progress_bar
9599

96100
self._ready = False
97101
self._ready_event = Event()
@@ -567,20 +571,21 @@ def wall_time(self):
567571
return self.timedelta(self.submitted, self.received)
568572

569573
def wait_interactive(self, interval=1.0, timeout=-1):
570-
"""interactive wait, printing progress at regular intervals"""
574+
"""interactive wait, printing progress at regular intervals."""
571575
if timeout is None:
572576
timeout = -1
573577
N = len(self)
574-
tic = time.time()
575-
while not self.ready() and (timeout < 0 or time.time() - tic <= timeout):
578+
tic = time.perf_counter()
579+
progress_bar = self.progress_bar(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+
):
576584
self.wait(interval)
577-
clear_output(wait=True)
578-
print(
579-
"%4i/%i tasks finished after %4i s" % (self.progress, N, self.elapsed),
580-
end="",
581-
)
582-
sys.stdout.flush()
583-
print("\ndone")
585+
progress_bar.update(self.progress - n_prev)
586+
n_prev = self.progress
587+
588+
progress_bar.close()
584589

585590
def _republish_displaypub(self, content, eid):
586591
"""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)