Browse code

progress: rate limit how often progress is displayed

Progress is displayed as fast as possible, which can actually rate-limit your
I/O, depending on how fast your progress output file can flush. i.e. if you're
using a very slow terminal, rendering progress could actually throttle your
upload/download rate.

This rate-limits the progress display to every second. Perhaps this should be
tunable, but I'm keeping this patch simple for now.

Signed-off-by: Steven Noonan <steven@uplinklabs.net>

Steven Noonan authored on 2012/10/07 08:13:44
Showing 1 changed files
... ...
@@ -5,10 +5,12 @@
5 5
 
6 6
 import sys
7 7
 import datetime
8
+import time
8 9
 import Utils
9 10
 
10 11
 class Progress(object):
11 12
     _stdout = sys.stdout
13
+    _last_display = 0
12 14
 
13 15
     def __init__(self, labels, total_size):
14 16
         self._stdout = sys.stdout
... ...
@@ -48,6 +50,13 @@ class Progress(object):
48 48
         self._stdout.write(u"%(source)s -> %(destination)s  %(extra)s\n" % self.labels)
49 49
         self._stdout.flush()
50 50
 
51
+    def _display_needed(self):
52
+        # We only need to update the display every so often.
53
+        if time.time() - self._last_display > 1:
54
+            self._last_display = time.time()
55
+            return True
56
+        return False
57
+
51 58
     def display(self, new_file = False, done_message = None):
52 59
         """
53 60
         display(new_file = False[/True], done = False[/True])
... ...
@@ -98,6 +107,10 @@ class ProgressANSI(Progress):
98 98
             self._stdout.flush()
99 99
             return
100 100
 
101
+        # Only display progress every so often
102
+        if not (new_file or done_message) and not self._display_needed():
103
+            return
104
+
101 105
         timedelta = self.time_current - self.time_start
102 106
         sec_elapsed = timedelta.days * 86400 + timedelta.seconds + float(timedelta.microseconds)/1000000.0
103 107
         if (sec_elapsed > 0):
... ...
@@ -132,6 +145,10 @@ class ProgressCR(Progress):
132 132
             self.output_labels()
133 133
             return
134 134
 
135
+        # Only display progress every so often
136
+        if not (new_file or done_message) and not self._display_needed():
137
+            return
138
+
135 139
         timedelta = self.time_current - self.time_start
136 140
         sec_elapsed = timedelta.days * 86400 + timedelta.seconds + float(timedelta.microseconds)/1000000.0
137 141
         if (sec_elapsed > 0):