Skip to content

Commit

Permalink
simplify unconditional use of tqdm
Browse files Browse the repository at this point in the history
  • Loading branch information
minrk committed Jun 7, 2021
1 parent 2f52cfc commit 37f1722
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
45 changes: 17 additions & 28 deletions ipyparallel/client/asyncresult.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
from Queue import Queue

from decorator import decorator
import tqdm
import zmq
from zmq import MessageTracker

from IPython import get_ipython
from IPython.core.display import clear_output, display, display_pretty
from IPython.core.display import display, display_pretty
from ipyparallel import error
from ipyparallel.util import utcnow, compare_datetimes, _parse_date
from ipython_genutils.py3compat import string_types
Expand Down Expand Up @@ -57,7 +58,8 @@ def check_ready(f, self, *args, **kwargs):
class AsyncResult(Future):
"""Class for representing results of non-blocking calls.
Provides the same interface as :py:class:`multiprocessing.pool.AsyncResult`.
Extends the interfaces of :py:class:`multiprocessing.pool.AsyncResult`
and :py:class:`concurrent.futures.Future`.
"""

msg_ids = None
Expand All @@ -73,6 +75,7 @@ def __init__(
fname='unknown',
targets=None,
owner=False,
progress_bar=tqdm.tqdm,
):
super(AsyncResult, self).__init__()
if not isinstance(children, list):
Expand All @@ -92,6 +95,7 @@ def __init__(
self._fname = fname
self._targets = targets
self.owner = owner
self.progress_bar = progress_bar

self._ready = False
self._ready_event = Event()
Expand Down Expand Up @@ -566,37 +570,22 @@ def wall_time(self):
"""
return self.timedelta(self.submitted, self.received)

def wait_interactive(self, interval=1.0, timeout=-1, progress=None):
"""interactive wait, printing progress at regular intervals.
progress can be a tqdm-like progress bar."""

use_progressbar = progress is not None
def wait_interactive(self, interval=1.0, timeout=-1):
"""interactive wait, printing progress at regular intervals."""
if timeout is None:
timeout = -1
N = len(self)
tic = time.time()
if use_progressbar:
progress_bar = progress(total=N)
n_prev = 0
while not self.ready() and (timeout < 0 or time.time() - tic <= timeout):
tic = time.perf_counter()
progress_bar = self.progress_bar(total=N, unit='tasks', desc=self._fname)
n_prev = 0
while not self.ready() and (
timeout < 0 or time.perf_counter() - tic <= timeout
):
self.wait(interval)
if use_progressbar:
progress_bar.update(self.progress - n_prev)
n_prev = self.progress
else:
clear_output(wait=True)
print(
"%4i/%i tasks finished after %4i s"
% (self.progress, N, self.elapsed),
end="",
)
sys.stdout.flush()
progress_bar.update(self.progress - n_prev)
n_prev = self.progress

if use_progressbar:
progress_bar.close()
else:
print("\ndone")
progress_bar.close()

def _republish_displaypub(self, content, eid):
"""republish individual displaypub content dicts"""
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def run(self):
"ipykernel>=4.4",
"tornado>=5.1",
"python-dateutil>=2.1",
"tqdm",
],
python_requires=">=3.6",
extras_require={
Expand Down

0 comments on commit 37f1722

Please sign in to comment.