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).""" |
read_set = [] |
write_set = [] |
stdout = None |
stderr = None |
|
if self.stdin: |
|
|
self.stdin.flush() |
if input: |
write_set.append(self.stdin) |
else: |
self.stdin.close() |
if self.stdout: |
read_set.append(self.stdout) |
stdout = [] |
if self.stderr: |
read_set.append(self.stderr) |
stderr = [] |
|
while read_set or write_set: |
rlist, wlist, xlist = select.select(read_set, write_set, []) |
|
if self.stdin in wlist: |
|
|
|
bytes_written = os.write(self.stdin.fileno(), input[:512]) |
input = input[bytes_written:] |
if not input: |
self.stdin.close() |
write_set.remove(self.stdin) |
|
if self.stdout in rlist: |
data = os.read(self.stdout.fileno(), 1024) |
if data == "": |
self.stdout.close() |
read_set.remove(self.stdout) |
stdout.append(data) |
|
if self.stderr in rlist: |
data = os.read(self.stderr.fileno(), 1024) |
if data == "": |
self.stderr.close() |
read_set.remove(self.stderr) |
stderr.append(data) |
|
|
if stdout != None: |
stdout = ''.join(stdout) |
if stderr != None: |
stderr = ''.join(stderr) |
|
|
|
|
|
if self.universal_newlines and hasattr(open, 'newlines'): |
if stdout: |
stdout = self._translate_newlines(stdout) |
if stderr: |
stderr = self._translate_newlines(stderr) |
|
-> self.wait() |
return (stdout, stderr) |