Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(43)

Unified Diff: gclient_utils.py

Issue 3342024: Add automatic auto-flushing stdout. (Closed)
Patch Set: update unit test Created 10 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « gclient.py ('k') | tests/gclient_utils_test.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: gclient_utils.py
diff --git a/gclient_utils.py b/gclient_utils.py
index d21ade51b0015943efcf310b674250911beeb155..9d14bd2554889e0d9bce345657990bca19b22b01 100644
--- a/gclient_utils.py
+++ b/gclient_utils.py
@@ -285,6 +285,30 @@ def CheckCallAndFilterAndHeader(args, always=False, **kwargs):
return CheckCallAndFilter(args, **kwargs)
+class StdoutAutoFlush(object):
+ """Automatically flush after N seconds."""
+ def __init__(self, stdout, delay=10):
+ self.lock = threading.Lock()
+ self.stdout = stdout
+ self.delay = delay
+ self.last_flushed_at = time.time()
+ self.stdout.flush()
+
+ def write(self, out):
+ """Thread-safe."""
+ self.stdout.write(out)
+ should_flush = False
+ with self.lock:
+ if (time.time() - self.last_flushed_at) > self.delay:
+ should_flush = True
+ self.last_flushed_at = time.time()
+ if should_flush:
+ self.stdout.flush()
+
+ def flush(self):
+ self.stdout.flush()
+
+
def CheckCallAndFilter(args, stdout=None, filter_fn=None,
print_stdout=None, call_filter_on_first_line=False,
**kwargs):
@@ -308,7 +332,6 @@ def CheckCallAndFilter(args, stdout=None, filter_fn=None,
**kwargs)
# Do a flush of stdout before we begin reading from the subprocess's stdout
- last_flushed_at = time.time()
stdout.flush()
# Also, we need to forward stdout to prevent weird re-ordering of output.
@@ -329,12 +352,6 @@ def CheckCallAndFilter(args, stdout=None, filter_fn=None,
else:
filter_fn(in_line)
in_line = ''
- # Flush at least 10 seconds between line writes. We wait at least 10
- # seconds to avoid overloading the reader that called us with output,
- # which can slow busy readers down.
- if (time.time() - last_flushed_at) > 10:
- last_flushed_at = time.time()
- stdout.flush()
in_byte = kid.stdout.read(1)
# Flush the rest of buffered output. This is only an issue with
# stdout/stderr not ending with a \n.
« no previous file with comments | « gclient.py ('k') | tests/gclient_utils_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698