MCPcopy Index your code
hub / github.com/python/cpython / communicate

Method communicate

Lib/subprocess.py:1230–1298  ·  view source on GitHub ↗

Interact with process: Send data to stdin and close it. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate. The optional "input" argument should be data to be sent to the child process, or None, if no data should be sent to

(self, input=None, timeout=None)

Source from the content-addressed store, hash-verified

1228 raise
1229
1230 def communicate(self, input=None, timeout=None):
1231 """Interact with process: Send data to stdin and close it.
1232 Read data from stdout and stderr, until end-of-file is
1233 reached. Wait for process to terminate.
1234
1235 The optional "input" argument should be data to be sent to the
1236 child process, or None, if no data should be sent to the child.
1237 communicate() returns a tuple (stdout, stderr).
1238
1239 By default, all communication is in bytes, and therefore any
1240 "input" should be bytes, and the (stdout, stderr) will be bytes.
1241 If in text mode (indicated by self.text_mode), any "input" should
1242 be a string, and (stdout, stderr) will be strings decoded
1243 according to locale encoding, or by "encoding" if set. Text mode
1244 is triggered by setting any of text, encoding, errors or
1245 universal_newlines.
1246 """
1247
1248 if self._communication_started and input:
1249 raise ValueError("Cannot send input after starting communication")
1250
1251 # Optimization: If we are not worried about timeouts, we haven't
1252 # started communicating, and we have one or zero pipes, using select()
1253 # or threads is unnecessary.
1254 if (timeout is None and not self._communication_started and
1255 [self.stdin, self.stdout, self.stderr].count(None) >= 2):
1256 stdout = None
1257 stderr = None
1258 if self.stdin:
1259 self._stdin_write(input)
1260 elif self.stdout:
1261 stdout = self.stdout.read()
1262 self.stdout.close()
1263 elif self.stderr:
1264 stderr = self.stderr.read()
1265 self.stderr.close()
1266 self.wait()
1267 else:
1268 if timeout is not None:
1269 endtime = _time() + timeout
1270 else:
1271 endtime = None
1272
1273 try:
1274 stdout, stderr = self._communicate(input, endtime, timeout)
1275 except KeyboardInterrupt:
1276 # https://bugs.python.org/issue25942
1277 # See the detailed comment in .wait().
1278 if timeout is not None:
1279 sigint_timeout = min(self._sigint_wait_secs,
1280 self._remaining_time(endtime))
1281 else:
1282 sigint_timeout = self._sigint_wait_secs
1283 self._sigint_wait_secs = 0 # nothing else should wait.
1284 try:
1285 self._wait(timeout=sigint_timeout)
1286 except TimeoutExpired:
1287 pass

Calls 8

_stdin_writeMethod · 0.95
waitMethod · 0.95
_communicateMethod · 0.95
_remaining_timeMethod · 0.95
_waitMethod · 0.95
countMethod · 0.45
readMethod · 0.45
closeMethod · 0.45