Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 """Syncronized Standard IO Linebuffer implemented with cStringIO.""" | 4 """Syncronized Standard IO Linebuffer implemented with cStringIO.""" |
| 5 | 5 |
| 6 import cStringIO | 6 import cStringIO |
| 7 import os | |
| 8 import sys | |
| 9 import threading | 7 import threading |
| 10 import Queue | 8 import Queue |
| 11 | 9 |
| 12 | 10 |
| 13 class StdioBuffer(object): | 11 class StdioBuffer(object): |
| 14 def __init__(self, shard): | 12 def __init__(self, shard): |
| 15 self.queue = Queue.Queue() | 13 self.queue = Queue.Queue() |
| 16 self.completed = 0 | 14 self.completed = 0 |
| 17 self.shard = shard | 15 self.shard = shard |
| 18 | 16 |
| 19 def _pipe_handler(self, system_pipe, program_pipe): | 17 def _pipe_handler(self, system_pipe, program_pipe): |
| 20 """Helper method for collecting stdio output. Output is collected until | 18 """Helper method for collecting stdio output. Output is collected until |
| 21 a newline is seen, at which point an event is triggered and the line is | 19 a newline is seen, at which point an event is triggered and the line is |
| 22 pushed to a buffer as a (stdio, line) tuple.""" | 20 pushed to a buffer as a (stdio, line) tuple.""" |
| 23 buffer = cStringIO.StringIO() | 21 buf = cStringIO.StringIO() |
|
csharp
2013/01/29 01:34:18
Why buffer to buf?
| |
| 24 pipe_running = True | 22 pipe_running = True |
| 25 while pipe_running: | 23 while pipe_running: |
| 26 char = program_pipe.read(1) | 24 char = program_pipe.read(1) |
| 27 if not char and self.shard.poll() is not None: | 25 if not char and self.shard.poll() is not None: |
| 28 pipe_running = False | 26 pipe_running = False |
| 29 buffer.write(char) | 27 buf.write(char) |
| 30 if char == '\n' or not pipe_running: | 28 if char == '\n' or not pipe_running: |
| 31 line = buffer.getvalue() | 29 line = buf.getvalue() |
| 32 if line: | 30 if line: |
| 33 self.queue.put((system_pipe, line)) | 31 self.queue.put((system_pipe, line)) |
| 34 if not pipe_running: | 32 if not pipe_running: |
| 35 self.queue.put((system_pipe, None)) | 33 self.queue.put((system_pipe, None)) |
| 36 buffer.close() | 34 buf.close() |
| 37 buffer = cStringIO.StringIO() | 35 buf = cStringIO.StringIO() |
| 38 | 36 |
| 39 def handle_pipe(self, system_pipe, program_pipe): | 37 def handle_pipe(self, system_pipe, program_pipe): |
| 40 t = threading.Thread(target=self._pipe_handler, args=[system_pipe, | 38 t = threading.Thread(target=self._pipe_handler, args=[system_pipe, |
| 41 program_pipe]) | 39 program_pipe]) |
| 42 t.start() | 40 t.start() |
| 43 return t | 41 return t |
| 44 | 42 |
| 45 def readline(self): | 43 def readline(self): |
| 46 """Emits a tuple of (sys.stderr, line), (sys.stdout, line), or (None, None) | 44 """Emits a tuple of (sys.stderr, line), (sys.stdout, line), or (None, None) |
| 47 if the process has finished. This is a blocking call.""" | 45 if the process has finished. This is a blocking call.""" |
| 48 while True: | 46 while True: |
| 49 (pipe, line) = self.queue.get(True) | 47 (pipe, line) = self.queue.get(True) |
| 50 if line: | 48 if line: |
| 51 return (pipe, line) | 49 return (pipe, line) |
| 52 self.completed += 1 | 50 self.completed += 1 |
| 53 if self.completed >= 2: | 51 if self.completed >= 2: |
| 54 return (None, None) | 52 return (None, None) |
| OLD | NEW |