Skip to content

Commit 144fd85

Browse files
author
Tim Vaillancourt
committed
parse output from Popen PIPE every poll to make sure the pipe buffer never fills. Moved polling to 250ms. Moved output to an array to facilitate changes
1 parent 8180815 commit 144fd85

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

MongoBackup/Common/LocalCommand.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def __init__(self, command, command_flags=[], verbose=False):
1010
self.command_flags = command_flags
1111
self.verbose = verbose
1212

13-
self.output = ""
13+
self.output = []
1414
self.stdout = None
1515
self.stderr = None
1616
self._process = None
@@ -26,7 +26,8 @@ def parse_output(self):
2626
output = self.stdout.strip()
2727
if output == "" and self.stderr.strip() != "":
2828
output = self.stderr.strip()
29-
self.output = "\n\t".join(output.split("\n"))
29+
if not output == "":
30+
self.output.append("\n\t".join(output.split("\n")))
3031
except Exception, e:
3132
raise Exception, "Error parsing output: %s" % e, None
3233
return self.output
@@ -35,22 +36,22 @@ def run(self):
3536
try:
3637
self._process = Popen(self.command_line, stdout=PIPE, stderr=PIPE)
3738
while self._process.poll() is None:
38-
sleep(0.5)
39+
self.parse_output()
40+
sleep(0.25)
3941
except Exception, e:
4042
raise e
4143

42-
self.parse_output()
4344
if self._process.returncode != 0:
4445
raise Exception, "%s command failed with exit code %i! Stderr output:\n%s" % (
4546
self.command,
4647
self._process.returncode,
4748
self.stderr.strip()
4849
), None
4950
elif self.verbose:
50-
if self.output == "":
51-
logging.debug("%s command completed" % (self.command))
51+
if len(self.output) > 0:
52+
logging.debug("%s command completed with output:\n\t%s" % (self.command, "\n".join(self.output)))
5253
else:
53-
logging.debug("%s command completed with output:\n\t%s" % (self.command, self.output))
54+
logging.debug("%s command completed" % (self.command))
5455

5556
def close(self):
5657
if self._process:

0 commit comments

Comments
 (0)