@@ -240,10 +240,34 @@ their completion.
240240 Note, that the data read is buffered in memory, so do not use
241241 this method if the data size is large or unlimited.
242242
243+ If this coroutine is cancelled (for example, when a timeout is
244+ set with :func: `~asyncio.wait_for `), the output that was already
245+ read is not lost: call :meth: `!communicate ` again to read the
246+ remaining output and get the complete data::
247+
248+ try:
249+ stdout, stderr = await asyncio.wait_for(
250+ proc.communicate(), timeout=5.0)
251+ except TimeoutError:
252+ proc.kill()
253+ stdout, stderr = await proc.communicate()
254+
255+ Passing *input * after a previous :meth: `!communicate ` call was
256+ cancelled raises :exc: `ValueError `; pass ``input=None `` to
257+ continue the communication, the original *input * is used.
258+
243259 .. versionchanged :: 3.12
244260
245261 *stdin * gets closed when ``input=None `` too.
246262
263+ .. versionchanged :: next
264+
265+ If :meth: `!communicate ` is cancelled, the output that was
266+ already read is now preserved and returned by a subsequent
267+ :meth: `!communicate ` call. Passing *input * to a
268+ :meth: `!communicate ` call following a cancelled one now raises
269+ :exc: `ValueError `.
270+
247271 .. method :: send_signal(signal)
248272
249273 Sends the signal *signal * to the child process.
0 commit comments