-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubprocessCommunicateAsync.py
More file actions
33 lines (25 loc) · 975 Bytes
/
SubprocessCommunicateAsync.py
File metadata and controls
33 lines (25 loc) · 975 Bytes
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
#!/usr/bin/env python
#
# \file SubprocessCommunicateAsync.py
# \brief Constantly print Subprocess output while process is running
#
# https://newbedev.com/constantly-print-subprocess-output-while-process-is-running
# https://www.endpoint.com/blog/2015/01/getting-realtime-output-using-python/
#
import sys
import subprocess
#--------------------------------------------------------------------------------------------------
def run(self, a_cmd):
proc = subprocess.Popen(a_cmd, shell = False, bufsize = 0, stdout = subprocess.PIPE, stderr = subprocess.STDOUT, encoding='utf8')
# Poll process for new output until finished
while True:
nextLine = proc.stdout.readline()
if (nextLine == "" and
proc.poll() is not None
):
break
sys.stdout.write(nextLine)
sys.stdout.flush()
stdOut, stdErr = proc.communicate()
return proc.returncode, stdOut, stdErr
#--------------------------------------------------------------------------------------------------