From f1f6ea57da00d4092251c3d69919bfc48b4f4486 Mon Sep 17 00:00:00 2001 From: aduriseti Date: Mon, 26 Sep 2016 21:17:01 -0700 Subject: [PATCH] prevents I/O from dominating computation time when using progress --- progress/__init__.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/progress/__init__.py b/progress/__init__.py index f886957..0fc8280 100644 --- a/progress/__init__.py +++ b/progress/__init__.py @@ -66,18 +66,20 @@ class Infinite(object): pass def next(self, n=1): - if n > 0: - now = time() - dt = now - self._ts - if dt < self.time_threshold: - self._pending += n - else: - self._xput.append((n + self._pending) / dt) - self._ts = now - self._pending = 0 - + if n <= 0: + return + now = time() + dt = now - self._ts + if dt < self.time_threshold: + self._pending += n + else: + # avoid performing computationally intensive update task + # more than 1/dt (10 by default) times a second + self._xput.append((self._pending + n) / dt) + self._ts = now + self._pending = 0 + self.update() self.index = self.index + n - self.update() def iter(self, it): try: