Skip to content

Commit 336dbe5

Browse files
vstinnersorcio
andauthored
[3.11] gh-108973: Fix asyncio SubprocessProtocol doc (#109431) (#109610)
gh-108973: Fix asyncio SubprocessProtocol doc (#109431) SubprocessProtocol process_exited() method can be called before pipe_data_received() and pipe_connection_lost() methods. Document it and adapt the example in the doc. Co-authored-by: Davide Rizzo <[email protected]> (cherry picked from commit ced6924)
1 parent 17a335d commit 336dbe5

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

Doc/library/asyncio-llapi-index.rst

+5-5
Original file line numberDiff line numberDiff line change
@@ -484,19 +484,19 @@ Protocol classes can implement the following **callback methods**:
484484
:widths: 50 50
485485
:class: full-width-table
486486

487-
* - ``callback`` :meth:`pipe_data_received()
488-
<SubprocessProtocol.pipe_data_received>`
487+
* - ``callback`` :meth:`~SubprocessProtocol.pipe_data_received`
489488
- Called when the child process writes data into its
490489
*stdout* or *stderr* pipe.
491490

492-
* - ``callback`` :meth:`pipe_connection_lost()
493-
<SubprocessProtocol.pipe_connection_lost>`
491+
* - ``callback`` :meth:`~SubprocessProtocol.pipe_connection_lost`
494492
- Called when one of the pipes communicating with
495493
the child process is closed.
496494

497495
* - ``callback`` :meth:`process_exited()
498496
<SubprocessProtocol.process_exited>`
499-
- Called when the child process has exited.
497+
- Called when the child process has exited. It can be called before
498+
:meth:`~SubprocessProtocol.pipe_data_received` and
499+
:meth:`~SubprocessProtocol.pipe_connection_lost` methods.
500500

501501

502502
Event Loop Policies

Doc/library/asyncio-protocol.rst

+18-1
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,9 @@ factories passed to the :meth:`loop.subprocess_exec` and
708708

709709
Called when the child process has exited.
710710

711+
It can be called before :meth:`~SubprocessProtocol.pipe_data_received` and
712+
:meth:`~SubprocessProtocol.pipe_connection_lost` methods.
713+
711714

712715
Examples
713716
========
@@ -1003,12 +1006,26 @@ The subprocess is created by the :meth:`loop.subprocess_exec` method::
10031006
def __init__(self, exit_future):
10041007
self.exit_future = exit_future
10051008
self.output = bytearray()
1009+
self.pipe_closed = False
1010+
self.exited = False
1011+
1012+
def pipe_connection_lost(self, fd, exc):
1013+
self.pipe_closed = True
1014+
self.check_for_exit()
10061015

10071016
def pipe_data_received(self, fd, data):
10081017
self.output.extend(data)
10091018

10101019
def process_exited(self):
1011-
self.exit_future.set_result(True)
1020+
self.exited = True
1021+
# process_exited() method can be called before
1022+
# pipe_connection_lost() method: wait until both methods are
1023+
# called.
1024+
self.check_for_exit()
1025+
1026+
def check_for_exit(self):
1027+
if self.pipe_closed and self.exited:
1028+
self.exit_future.set_result(True)
10121029

10131030
async def get_date():
10141031
# Get a reference to the event loop as we plan to use

0 commit comments

Comments
 (0)