2
2
# based on asyncio library:
3
3
# Copyright (C) 2001 Python Software Foundation
4
4
#
5
+ # Copyright (C) 2024 Marek Marczykowski-Górecki
6
+ # <marmarek@invisiblethingslab.com>
5
7
#
6
8
7
9
import collections
8
10
from asyncio import protocols , events
9
11
10
-
11
- class FlowControlMixin (protocols .Protocol ):
12
+ class StdoutWriterProtocol (protocols .Protocol ):
12
13
"""Reusable flow control logic for StreamWriter.drain().
13
-
14
14
This implements the protocol methods pause_writing(),
15
15
resume_writing() and connection_lost(). If the subclass overrides
16
16
these it must call the super methods.
17
-
18
17
StreamWriter.drain() must wait for _drain_helper() coroutine.
19
18
"""
20
19
@@ -26,18 +25,15 @@ def __init__(self, loop=None):
26
25
self ._paused = False
27
26
self ._drain_waiters = collections .deque ()
28
27
self ._connection_lost = False
28
+ self ._closed = self ._loop .create_future ()
29
29
30
30
def pause_writing (self ):
31
31
assert not self ._paused
32
32
self ._paused = True
33
- if self ._loop .get_debug ():
34
- logger .debug ("%r pauses writing" , self )
35
33
36
34
def resume_writing (self ):
37
35
assert self ._paused
38
36
self ._paused = False
39
- if self ._loop .get_debug ():
40
- logger .debug ("%r resumes writing" , self )
41
37
42
38
for waiter in self ._drain_waiters :
43
39
if not waiter .done ():
@@ -55,6 +51,11 @@ def connection_lost(self, exc):
55
51
waiter .set_result (None )
56
52
else :
57
53
waiter .set_exception (exc )
54
+ if not self ._closed .done ():
55
+ if exc is None :
56
+ self ._closed .set_result (None )
57
+ else :
58
+ self ._closed .set_exception (exc )
58
59
59
60
async def _drain_helper (self ):
60
61
if self ._connection_lost :
@@ -68,6 +69,6 @@ async def _drain_helper(self):
68
69
finally :
69
70
self ._drain_waiters .remove (waiter )
70
71
72
+ # pylint: disable=unused-argument
71
73
def _get_close_waiter (self , stream ):
72
- raise NotImplementedError
73
-
74
+ return self ._closed
0 commit comments