daemon: Used the communicate() method to interact with subprocesses.

This commit is contained in:
Tom Goff 2016-01-29 16:43:46 -05:00
parent 6fb1eb9bd6
commit 0333c74bec
3 changed files with 8 additions and 21 deletions

View file

@ -27,13 +27,8 @@ def createngnode(type, hookstr, name=None):
cmd = [NGCTL_BIN, "-f", "-"] cmd = [NGCTL_BIN, "-f", "-"]
cmdid = subprocess.Popen(cmd, stdin = subprocess.PIPE, cmdid = subprocess.Popen(cmd, stdin = subprocess.PIPE,
stdout = subprocess.PIPE, stdout = subprocess.PIPE,
stderr = subprocess.PIPE) stderr = subprocess.STDOUT)
cmdid.stdin.write(ngcmd) result, err = cmdid.communicate(input = ngcmd) # err will always be None
cmdid.stdin.close()
result = cmdid.stdout.read()
result += cmdid.stderr.read()
cmdid.stdout.close()
cmdid.stderr.close()
status = cmdid.wait() status = cmdid.wait()
if status > 0: if status > 0:
raise Exception, "error creating Netgraph node %s (%s): %s" % \ raise Exception, "error creating Netgraph node %s (%s): %s" % \

View file

@ -105,14 +105,10 @@ def cmdresult(args):
exit status and result string. stderr output exit status and result string. stderr output
is folded into the stdout result string. is folded into the stdout result string.
''' '''
cmdid = subprocess.Popen(args, stdin = subprocess.PIPE, cmdid = subprocess.Popen(args, stdin = open(os.devnull, 'r'),
stdout = subprocess.PIPE, stdout = subprocess.PIPE,
stderr = subprocess.PIPE) stderr = subprocess.STDOUT)
cmdid.stdin.close() result, err = cmdid.communicate() # err will always be None
result = cmdid.stdout.read()
result += cmdid.stderr.read()
cmdid.stdout.close()
cmdid.stderr.close()
status = cmdid.wait() status = cmdid.wait()
return (status, result) return (status, result)

View file

@ -90,14 +90,10 @@ class PhysicalNode(PyCoreNode):
''' '''
os.chdir(self.nodedir) os.chdir(self.nodedir)
# in Python 2.7 we can use subprocess.check_output() here # in Python 2.7 we can use subprocess.check_output() here
tmp = subprocess.Popen(args, stdin = subprocess.PIPE, tmp = subprocess.Popen(args, stdin = open(os.devnull, 'r'),
stdout = subprocess.PIPE, stdout = subprocess.PIPE,
stderr = subprocess.PIPE) stderr = subprocess.STDOUT)
result = tmp.stdout.read() result, err = tmp.communicate() # err will always be None
result += tmp.stderr.read()
tmp.stdin.close()
tmp.stdout.close()
tmp.stderr.close()
status = tmp.wait() status = tmp.wait()
return (status, result) return (status, result)