Skip to content
Merged
24 changes: 24 additions & 0 deletions Doc/library/subprocess.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,30 @@ handling consistency are valid for these functions.
Notes
-----

.. _subprocess-timeout-behavior:

Timeout Behavior
^^^^^^^^^^^^^^^^

When using the ``timeout`` parameter in functions like :func:`run`,
:meth:`Popen.wait`, or :meth:`Popen.communicate`,
users should be aware of the following behaviors:

Process Creation Delay
^^^^^^^^^^^^^^^^^^^^^^

The initial process creation itself cannot be interrupted on many platform APIs.
This means that even when specifying a timeout, you are not guaranteed to see a timeout exception
until at least after however long process creation takes.

Extremely Small Timeout Values
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Setting very small timeout values (such as a few milliseconds) may result
in almost immediate :exc:`TimeoutExpired` exceptions because
process creation and system scheduling inherently require time.


.. _converting-argument-sequence:

Converting an argument sequence to a string on Windows
Expand Down
6 changes: 4 additions & 2 deletions Lib/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -2145,8 +2145,10 @@ def _communicate(self, input, endtime, orig_timeout):
selector.unregister(key.fileobj)
key.fileobj.close()
self._fileobj2output[key.fileobj].append(data)

self.wait(timeout=self._remaining_time(endtime))
try:
self.wait(timeout=self._remaining_time(endtime))
except TimeoutExpired:
raise TimeoutExpired(self.args, orig_timeout)

# All data exchanged. Translate lists into strings.
if stdout is not None:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Use origional timeout value for :exc:`TimeoutExpired`
when the func :meth:`subprocess.run` is called with a timeout
Loading