1
1
from collections import OrderedDict , deque
2
2
from dataclasses import dataclass , field
3
- from typing import Deque , Generic , List , NamedTuple , TypeVar
3
+ from types import TracebackType
4
+ from typing import Deque , Generic , List , NamedTuple , Optional , Type , TypeVar
4
5
5
6
from .. import (
6
7
BrokenResourceError , ClosedResourceError , EndOfStream , WouldBlock , get_cancelled_exc_class )
@@ -112,7 +113,14 @@ def clone(self) -> 'MemoryObjectReceiveStream':
112
113
113
114
return MemoryObjectReceiveStream (_state = self ._state )
114
115
115
- async def aclose (self ) -> None :
116
+ def close (self ) -> None :
117
+ """
118
+ Close the stream.
119
+
120
+ This works the exact same way as :meth:`aclose`, but is provided as a special case for the
121
+ benefit of synchronous callbacks.
122
+
123
+ """
116
124
if not self ._closed :
117
125
self ._closed = True
118
126
self ._state .open_receive_channels -= 1
@@ -121,6 +129,9 @@ async def aclose(self) -> None:
121
129
for event in send_events :
122
130
event .set ()
123
131
132
+ async def aclose (self ) -> None :
133
+ self .close ()
134
+
124
135
def statistics (self ) -> MemoryObjectStreamStatistics :
125
136
"""
126
137
Return statistics about the current state of this stream.
@@ -129,6 +140,14 @@ def statistics(self) -> MemoryObjectStreamStatistics:
129
140
"""
130
141
return self ._state .statistics ()
131
142
143
+ def __enter__ (self ) -> 'MemoryObjectReceiveStream[T_Item]' :
144
+ return self
145
+
146
+ def __exit__ (self , exc_type : Optional [Type [BaseException ]],
147
+ exc_val : Optional [BaseException ],
148
+ exc_tb : Optional [TracebackType ]) -> None :
149
+ self .close ()
150
+
132
151
133
152
@dataclass (eq = False )
134
153
class MemoryObjectSendStream (Generic [T_Item ], ObjectSendStream [T_Item ]):
@@ -198,7 +217,14 @@ def clone(self) -> 'MemoryObjectSendStream':
198
217
199
218
return MemoryObjectSendStream (_state = self ._state )
200
219
201
- async def aclose (self ) -> None :
220
+ def close (self ) -> None :
221
+ """
222
+ Close the stream.
223
+
224
+ This works the exact same way as :meth:`aclose`, but is provided as a special case for the
225
+ benefit of synchronous callbacks.
226
+
227
+ """
202
228
if not self ._closed :
203
229
self ._closed = True
204
230
self ._state .open_send_channels -= 1
@@ -208,10 +234,21 @@ async def aclose(self) -> None:
208
234
for event in receive_events :
209
235
event .set ()
210
236
237
+ async def aclose (self ) -> None :
238
+ self .close ()
239
+
211
240
def statistics (self ) -> MemoryObjectStreamStatistics :
212
241
"""
213
242
Return statistics about the current state of this stream.
214
243
215
244
.. versionadded:: 3.0
216
245
"""
217
246
return self ._state .statistics ()
247
+
248
+ def __enter__ (self ) -> 'MemoryObjectSendStream[T_Item]' :
249
+ return self
250
+
251
+ def __exit__ (self , exc_type : Optional [Type [BaseException ]],
252
+ exc_val : Optional [BaseException ],
253
+ exc_tb : Optional [TracebackType ]) -> None :
254
+ self .close ()
0 commit comments