-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathget_process_output.py
55 lines (47 loc) · 1.56 KB
/
get_process_output.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import clr
import System
clr.AddReference("System.Core")
clr.ImportExtensions(System.Linq)
from System.Collections.Generic import Dictionary
from System.Threading.Tasks import Task
class StreamData(object):
def __init__(self, stream):
self.stream = stream
self.lines = []
return
def AddLine(self, line):
self.lines.append(line)
def GetLines(self):
return self.lines
def GetStream(self):
return self.stream
def ReadProcessOutputAndErrorLines(process, processOutput, processError, timeOutInMs=-1):
completed = False
outputData = StreamData(process.StandardOutput)
errorData = StreamData(process.StandardError)
task2stream = Dictionary[Task, StreamData]()
task2stream[outputData.GetStream().ReadLineAsync()] = outputData
task2stream[errorData.GetStream().ReadLineAsync()] = errorData
while True:
tasks = task2stream.Keys.ToArray[Task]()
index = Task.WaitAny(tasks, timeOutInMs)
if index == -1: # timed out!
completed = False
break
task = tasks[index]
line = task.Result
streamData = task2stream[task]
task2stream.Remove(task) # must remove the completed task.
if line is not None:
streamData.AddLine(line)
if streamData == outputData:
processOutput(line)
elif streamData == errorData:
processError(line)
else:
raise Exception("Unexpected program state!")
task2stream[streamData.GetStream().ReadLineAsync()] = streamData
if not task2stream.Any():
completed = True
break
return outputData.GetLines(), errorData.GetLines(), completed