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

Method _communicate

Lib/subprocess.py:1675–1738  ·  view source on GitHub ↗
(self, input, endtime, orig_timeout)

Source from the content-addressed store, hash-verified

1673
1674
1675 def _communicate(self, input, endtime, orig_timeout):
1676 # Start reader threads feeding into a list hanging off of this
1677 # object, unless they've already been started.
1678 if self.stdout and not hasattr(self, "_stdout_buff"):
1679 self._stdout_buff = []
1680 self.stdout_thread = \
1681 threading.Thread(target=self._readerthread,
1682 args=(self.stdout, self._stdout_buff))
1683 self.stdout_thread.daemon = True
1684 self.stdout_thread.start()
1685 if self.stderr and not hasattr(self, "_stderr_buff"):
1686 self._stderr_buff = []
1687 self.stderr_thread = \
1688 threading.Thread(target=self._readerthread,
1689 args=(self.stderr, self._stderr_buff))
1690 self.stderr_thread.daemon = True
1691 self.stderr_thread.start()
1692
1693 # Start writer thread to send input to stdin, unless already
1694 # started. The thread writes input and closes stdin when done,
1695 # or continues in the background on timeout.
1696 if self.stdin and not hasattr(self, "_stdin_thread"):
1697 self._stdin_thread = \
1698 threading.Thread(target=self._writerthread,
1699 args=(input,))
1700 self._stdin_thread.daemon = True
1701 self._stdin_thread.start()
1702
1703 # Wait for the writer thread, or time out. If we time out, the
1704 # thread remains writing and the fd left open in case the user
1705 # calls communicate again.
1706 if hasattr(self, "_stdin_thread"):
1707 self._stdin_thread.join(self._remaining_time(endtime))
1708 if self._stdin_thread.is_alive():
1709 raise TimeoutExpired(self.args, orig_timeout)
1710
1711 # Wait for the reader threads, or time out. If we time out, the
1712 # threads remain reading and the fds left open in case the user
1713 # calls communicate again.
1714 if self.stdout is not None:
1715 self.stdout_thread.join(self._remaining_time(endtime))
1716 if self.stdout_thread.is_alive():
1717 raise TimeoutExpired(self.args, orig_timeout)
1718 if self.stderr is not None:
1719 self.stderr_thread.join(self._remaining_time(endtime))
1720 if self.stderr_thread.is_alive():
1721 raise TimeoutExpired(self.args, orig_timeout)
1722
1723 # Collect the output from and close both pipes, now that we know
1724 # both have been read successfully.
1725 stdout = None
1726 stderr = None
1727 if self.stdout:
1728 stdout = self._stdout_buff
1729 self.stdout.close()
1730 if self.stderr:
1731 stderr = self._stderr_buff
1732 self.stderr.close()

Callers 1

communicateMethod · 0.95

Calls 15

_remaining_timeMethod · 0.95
_save_inputMethod · 0.95
_check_timeoutMethod · 0.95
waitMethod · 0.95
_translate_newlinesMethod · 0.95
TimeoutExpiredClass · 0.85
castMethod · 0.80
startMethod · 0.45
joinMethod · 0.45
is_aliveMethod · 0.45
closeMethod · 0.45
flushMethod · 0.45

Tested by

no test coverage detected