[Python-modules-commits] [python-spur] 04/13: Don't use a thread if using read call to read output

Ruben Undheim rubund-guest at moszumanska.debian.org
Sun Jan 3 10:38:51 UTC 2016


This is an automated email from the git hooks/post-receive script.

rubund-guest pushed a commit to branch master
in repository python-spur.

commit 4a1e84d22d7c315a7c5fb65ec607fb19425612ab
Author: Michael Williamson <mike at zwobble.org>
Date:   Sun Dec 27 22:46:05 2015 +0000

    Don't use a thread if using read call to read output
---
 spur/io.py | 56 ++++++++++++++++++++++++++++++++++----------------------
 1 file changed, 34 insertions(+), 22 deletions(-)

diff --git a/spur/io.py b/spur/io.py
index eb8edc6..04190f8 100644
--- a/spur/io.py
+++ b/spur/io.py
@@ -4,20 +4,42 @@ import threading
 class IoHandler(object):
     def __init__(self, in_out_pairs):
         self._handlers = [
-            OutputHandler(file_in, file_out)
+            _output_handler(file_in, file_out)
             for file_in, file_out
             in in_out_pairs
         ]
         
     def wait(self):
         return [handler.wait() for handler in self._handlers]
+
+
+def _output_handler(file_in, file_out):
+    if file_out is None:
+        return _ReadOutputAtEnd(file_in)
+    else:
+        return _ContinuousReader(file_in, file_out)
+
+
+class _ReadOutputAtEnd(object):
+    def __init__(self, file_in):
+        self._file_in = file_in
+    
+    def wait(self):
+        try:
+            return self._file_in.read()
+        except IOError:
+            # TODO: is there a more elegant fix?
+            # Attempting to read from a pty master that has received nothing
+            # seems to raise an IOError when reading
+            # See: http://bugs.python.org/issue5380
+            return b""
     
 
-class OutputHandler(object):
+class _ContinuousReader(object):
     def __init__(self, file_in, file_out):
         self._file_in = file_in
         self._file_out = file_out
-        self._output = ""
+        self._output = b""
         
         self._thread = threading.Thread(target=self._capture_output)
         self._thread.daemon = True
@@ -28,22 +50,12 @@ class OutputHandler(object):
         return self._output
     
     def _capture_output(self):
-        if self._file_out is None:
-            try:
-                self._output = self._file_in.read()
-            except IOError:
-                # TODO: is there a more elegant fix?
-                # Attempting to read from a pty master that has received nothing
-                # seems to raise an IOError when reading
-                # See: http://bugs.python.org/issue5380
-                self._output = b""
-        else:
-            output_buffer = []
-            while True:
-                output = self._file_in.read(1)
-                if output:
-                    self._file_out.write(output)
-                    output_buffer.append(output)
-                else:
-                    self._output = b"".join(output_buffer)
-                    return
+        output_buffer = []
+        while True:
+            output = self._file_in.read(1)
+            if output:
+                self._file_out.write(output)
+                output_buffer.append(output)
+            else:
+                self._output = b"".join(output_buffer)
+                return

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-spur.git



More information about the Python-modules-commits mailing list