changeset 10:dfefc456dc93

capture the output of the running executable (from a different thread)
author Dirk Olmes <dirk@xanthippe.ping.de>
date Sun, 21 Sep 2014 02:45:24 +0200
parents d666b9fe5663
children 191f6296a2a4
files RemoteViewer/Viewer.py
diffstat 1 files changed, 21 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/RemoteViewer/Viewer.py	Sat Sep 20 06:44:47 2014 +0200
+++ b/RemoteViewer/Viewer.py	Sun Sep 21 02:45:24 2014 +0200
@@ -1,13 +1,32 @@
 import subprocess
+from threading import Thread
+
+class ReadThread(Thread):
+    def __init__(self, output):
+        super(ReadThread, self).__init__()
+        self.daemon = True
+        self.output = output
+
+    def run(self):
+        for line in iter(self.output.readline, b''):
+            line = line.decode('utf-8')
+            line = line.rstrip('\n')
+            print(line)
 
 class AbstractViewer(object):
     def __init__(self,  connection):
         self.connection = connection
+        self.process = None
 
     def open(self):
         command = self.buildCommand()
-        print(command)
-        subprocess.call(command)
+        self.process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+        self.__startLogThread(self.process.stdout)
+        self.process.wait()
+
+    def __startLogThread(self, output):
+        readThread = ReadThread(output)
+        readThread.start()
 
 class VncViewer(AbstractViewer):
     def buildCommand(self):