Fix time() in refresh() + avoid racing conditions in write() (#264)

* Fix time() in refresh() + avoid racing conditions in write()
* Fix __repr__() using last_print_t instead of start_t #269
* Fix update(0) ZeroDivisionError if first update ever #268
This commit is contained in:
Stephen L 2016-09-15 04:06:21 +02:00 committed by GitHub
parent 0243499601
commit 2d45eca654
1 changed files with 9 additions and 4 deletions

View File

@ -340,7 +340,9 @@ class tqdm(object):
fp.write(end)
# Force refresh display of bars we cleared
for inst in inst_cleared:
inst.refresh()
# Avoid racing conditions by checking that the instance started
if hasattr(inst, 'started') and inst.started:
inst.refresh()
# TODO: make list of all instances incl. absolutely positioned ones?
@classmethod
@ -629,6 +631,9 @@ class tqdm(object):
# Init the time counter
self.start_t = self.last_print_t = self._time()
# Avoid race conditions by setting a flag at the very end of init
self.started = True
def __len__(self):
return (self.iterable.shape[0] if hasattr(self.iterable, 'shape')
else len(self.iterable)) if self.iterable is not None \
@ -646,7 +651,7 @@ class tqdm(object):
def __repr__(self):
return self.format_meter(self.n, self.total,
time() - self.last_print_t,
self._time() - self.start_t,
self.ncols, self.desc, self.ascii, self.unit,
self.unit_scale, 1 / self.avg_time
if self.avg_time else None, self.bar_format)
@ -723,7 +728,7 @@ Please use `tqdm_gui(...)` instead of `tqdm(..., gui=True)`
delta_it = n - last_print_n
elapsed = cur_t - start_t
# EMA (not just overall average)
if smoothing and delta_t:
if smoothing and delta_t and delta_it:
avg_time = delta_t / delta_it \
if avg_time is None \
else smoothing * delta_t / delta_it + \
@ -803,7 +808,7 @@ Please use `tqdm_gui(...)` instead of `tqdm(..., gui=True)`
delta_it = self.n - self.last_print_n # should be n?
elapsed = cur_t - self.start_t
# EMA (not just overall average)
if self.smoothing and delta_t:
if self.smoothing and delta_t and delta_it:
self.avg_time = delta_t / delta_it \
if self.avg_time is None \
else self.smoothing * delta_t / delta_it + \