diff options
author | Peter Astrand <astrand@lysator.liu.se> | 2005-03-03 20:28:59 (GMT) |
---|---|---|
committer | Peter Astrand <astrand@lysator.liu.se> | 2005-03-03 20:28:59 (GMT) |
commit | 23109f0009b34af04350417b17efc8190879ea78 (patch) | |
tree | 7c1ea1eb46cbdf230e38be6f1b2b81f00428f8ec /Lib/subprocess.py | |
parent | cbac93c2293dd2cc6f10b31e91eaa1a502822704 (diff) | |
download | cpython-23109f0009b34af04350417b17efc8190879ea78.zip cpython-23109f0009b34af04350417b17efc8190879ea78.tar.gz cpython-23109f0009b34af04350417b17efc8190879ea78.tar.bz2 |
Optimization for communicate(): If only one of stdin/stdout/stderr is
redirected, using select() or threads is unnecessary.
Diffstat (limited to 'Lib/subprocess.py')
-rw-r--r-- | Lib/subprocess.py | 45 |
1 files changed, 29 insertions, 16 deletions
diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 382acb6..61effde 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -619,6 +619,33 @@ class Popen(object): data = data.replace("\r", "\n") return data + def communicate(self, input=None): + """Interact with process: Send data to stdin. Read data from + stdout and stderr, until end-of-file is reached. Wait for + process to terminate. The optional input argument should be a + string to be sent to the child process, or None, if no data + should be sent to the child. + + communicate() returns a tuple (stdout, stderr).""" + + # Optimization: If we are only using one pipe, or no pipe at + # all, using select() or threads is unnecessary. + if [self.stdin, self.stdout, self.stderr].count(None) >= 2: + stdout = None + stderr = None + if self.stdin: + if input: + self.stdin.write(input) + self.stdin.close() + elif self.stdout: + stdout = self.stdout.read() + elif self.stderr: + stderr = self.stderr.read() + self.wait() + return (stdout, stderr) + + return self._communicate(input) + if mswindows: # @@ -811,14 +838,7 @@ class Popen(object): buffer.append(fh.read()) - def communicate(self, input=None): - """Interact with process: Send data to stdin. Read data from - stdout and stderr, until end-of-file is reached. Wait for - process to terminate. The optional input argument should be a - string to be sent to the child process, or None, if no data - should be sent to the child. - - communicate() returns a tuple (stdout, stderr).""" + def _communicate(self, input): stdout = None # Return stderr = None # Return @@ -1066,14 +1086,7 @@ class Popen(object): return self.returncode - def communicate(self, input=None): - """Interact with process: Send data to stdin. Read data from - stdout and stderr, until end-of-file is reached. Wait for - process to terminate. The optional input argument should be a - string to be sent to the child process, or None, if no data - should be sent to the child. - - communicate() returns a tuple (stdout, stderr).""" + def _communicate(self, input): read_set = [] write_set = [] stdout = None # Return |