Skip to content

Commit 3093386

Browse files
authored
Merge pull request #528 from minrk/tqdm-races
avoid races in progress bars
2 parents a4270e9 + 43a241e commit 3093386

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

ipyparallel/client/asyncresult.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -620,14 +620,13 @@ def wait_interactive(self, interval=0.1, timeout=-1, widget=None):
620620
tic = time.perf_counter()
621621
progress_bar = progress(widget=widget, total=N, unit='tasks', desc=self._fname)
622622

623-
n_prev = 0
624623
while not self.ready() and (
625624
timeout is None or time.perf_counter() - tic <= timeout
626625
):
627626
self.wait(interval)
628-
progress_bar.update(self.progress - n_prev)
629-
n_prev = self.progress
627+
progress_bar.update(self.progress - progress_bar.n)
630628

629+
progress_bar.update(self.progress - progress_bar.n)
631630
progress_bar.close()
632631

633632
def _republish_displaypub(self, content, eid):

ipyparallel/client/client.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -1381,17 +1381,21 @@ def wait_for_engines(
13811381

13821382
if interactive:
13831383
progress_bar = util.progress(
1384-
widget=widget, initial=len(self.ids), total=n, unit='engine'
1384+
widget=widget,
1385+
initial=len(self.ids),
1386+
total=n,
1387+
unit='engine',
13851388
)
13861389

13871390
future = Future()
13881391

13891392
def notify(_):
13901393
if future.done():
13911394
return
1395+
current_n = len(self.ids)
13921396
if interactive:
1393-
progress_bar.update(len(self.ids) - progress_bar.n)
1394-
if len(self.ids) >= n:
1397+
progress_bar.update(current_n - progress_bar.n)
1398+
if current_n >= n:
13951399
# ensure we refresh when we finish
13961400
if interactive:
13971401
progress_bar.close()
@@ -1405,12 +1409,13 @@ def on_timeout():
14051409
if future.done():
14061410
return
14071411

1408-
if len(self.ids) >= n:
1412+
current_n = len(self.ids)
1413+
if current_n >= n:
14091414
future.set_result(None)
14101415
else:
14111416
future.set_exception(
14121417
TimeoutError(
1413-
f"{n} engines not ready in {timeout} seconds. Currently ready: {len(self.ids)}"
1418+
f"{n} engines not ready in {timeout} seconds. Currently ready: {current_n}"
14141419
)
14151420
)
14161421

ipyparallel/util.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from signal import SIGTERM
2323
from types import FunctionType
2424

25-
import tqdm
2625
import traitlets
2726
import zmq
2827
from dateutil.parser import parse as dateutil_parse
@@ -657,8 +656,12 @@ def progress(*args, widget=None, **kwargs):
657656
else:
658657
widget = False
659658
if widget:
660-
f = tqdm.tqdm_notebook
659+
import tqdm.notebook
660+
661+
f = tqdm.notebook.tqdm_notebook
661662
else:
663+
import tqdm
664+
662665
kwargs.setdefault("file", sys.stdout)
663666
f = tqdm.tqdm
664667
return f(*args, **kwargs)

0 commit comments

Comments
 (0)