Skip to content

Commit 8f80732

Browse files
authored
Merge pull request #9 from timvaillancourt/subprocess_popen_stall
Subprocess.Popen stall fix for Issue #8
2 parents e37d65a + 73cf92a commit 8f80732

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

MongoBackup/Common/LocalCommand.py

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

13-
self.output = ""
14-
self.stdout = None
15-
self.stderr = None
13+
self.output = []
1614
self._process = None
1715

1816
self.command_line = [self.command]
@@ -22,11 +20,12 @@ def __init__(self, command, command_flags=[], verbose=False):
2220
def parse_output(self):
2321
if self._process:
2422
try:
25-
self.stdout, self.stderr = self._process.communicate()
26-
output = self.stdout.strip()
27-
if output == "" and self.stderr.strip() != "":
28-
output = self.stderr.strip()
29-
self.output = "\n\t".join(output.split("\n"))
23+
stdout, stderr = self._process.communicate()
24+
output = stdout.strip()
25+
if output == "" and stderr.strip() != "":
26+
output = stderr.strip()
27+
if not output == "":
28+
self.output.append("\n\t".join(output.split("\n")))
3029
except Exception, e:
3130
raise Exception, "Error parsing output: %s" % e, None
3231
return self.output
@@ -35,22 +34,22 @@ def run(self):
3534
try:
3635
self._process = Popen(self.command_line, stdout=PIPE, stderr=PIPE)
3736
while self._process.poll() is None:
38-
sleep(0.5)
37+
self.parse_output()
38+
sleep(0.1)
3939
except Exception, e:
4040
raise e
4141

42-
self.parse_output()
4342
if self._process.returncode != 0:
4443
raise Exception, "%s command failed with exit code %i! Stderr output:\n%s" % (
4544
self.command,
4645
self._process.returncode,
47-
self.stderr.strip()
46+
"\n".join(self.output)
4847
), None
4948
elif self.verbose:
50-
if self.output == "":
51-
logging.debug("%s command completed" % (self.command))
49+
if len(self.output) > 0:
50+
logging.debug("%s command completed with output:\n\t%s" % (self.command, "\n".join(self.output)))
5251
else:
53-
logging.debug("%s command completed with output:\n\t%s" % (self.command, self.output))
52+
logging.debug("%s command completed" % (self.command))
5453

5554
def close(self):
5655
if self._process:

0 commit comments

Comments
 (0)