Commit eef76ab7 authored by Xavier Thompson's avatar Xavier Thompson

SlapPopen: log output in realtime without a thread

In Python 3, a separate thread is not needed to log output in realtime.
See discussion in !319 for more details.

This partly reverts commit 86ce8b8e.
parent a4a25a39
...@@ -35,7 +35,6 @@ import pkg_resources ...@@ -35,7 +35,6 @@ import pkg_resources
import pwd import pwd
import stat import stat
import sys import sys
import threading
import logging import logging
import psutil import psutil
import time import time
...@@ -92,27 +91,6 @@ LOCALE_ENVIRONMENT_REMOVE_LIST = [ ...@@ -92,27 +91,6 @@ LOCALE_ENVIRONMENT_REMOVE_LIST = [
'LC_TIME', 'LC_TIME',
] ]
def logAndAccumulateOutput(process_stdout, buffer, logger):
"""Read process output and place the output in `buffer`, logging the lines
one by one as they are emitted.
"""
current_output = ''
while 1:
data = os.read(process_stdout.fileno(), 4096)
if not data:
return
data = data.decode('utf-8', 'replace')
buffer.append(data)
current_output += data
for current_output_line in current_output.splitlines(True):
if current_output_line.endswith('\n'):
logger.info(current_output_line.rstrip('\n'))
current_output = ''
else:
current_output = current_output_line
class SlapPopen(subprocess.Popen): class SlapPopen(subprocess.Popen):
""" """
Almost normal subprocess with greedish features and logging. Almost normal subprocess with greedish features and logging.
...@@ -151,17 +129,12 @@ class SlapPopen(subprocess.Popen): ...@@ -151,17 +129,12 @@ class SlapPopen(subprocess.Popen):
self.stdin = None self.stdin = None
output_lines = [] output_lines = []
for line in self.stdout:
# BBB: reading output in a separate thread is not needed on python 3, if type(line) is not str:
# iterating on self.stdout seems enough. line = line.decode(errors='replace')
t = threading.Thread( output_lines.append(line)
target=logAndAccumulateOutput, logger.info(line.rstrip('\n'))
args=(self.stdout, output_lines, logger)) self.wait(timeout=timeout)
t.start()
try:
self.wait(timeout=timeout)
finally:
t.join()
self.output = ''.join(output_lines) self.output = ''.join(output_lines)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment