@@ -7,10 +7,10 @@ Streams
7
7
=======
8
8
9
9
Streams are high-level async/await-ready primitives to work with
10
- network connections. Streams allow send and receive data without
10
+ network connections. Streams allow sending and receiving data without
11
11
using callbacks or low-level protocols and transports.
12
12
13
- Here's an example of a TCP echo client written using asyncio
13
+ Here is an example of a TCP echo client written using asyncio
14
14
streams::
15
15
16
16
import asyncio
@@ -31,6 +31,9 @@ streams::
31
31
asyncio.run(tcp_echo_client('Hello World!'))
32
32
33
33
34
+ See also the `Examples `_ section below.
35
+
36
+
34
37
.. rubric :: Stream Functions
35
38
36
39
The following top-level asyncio functions can be used to create
@@ -43,7 +46,7 @@ and work with streams:
43
46
server_hostname=None, ssl_handshake_timeout=None)
44
47
45
48
Establish a network connection and return a pair of
46
- ``(reader, writer) ``.
49
+ ``(reader, writer) `` objects .
47
50
48
51
The returned *reader * and *writer * objects are instances of
49
52
:class: `StreamReader ` and :class: `StreamWriter ` classes.
@@ -52,7 +55,8 @@ and work with streams:
52
55
automatically when this method is awaited from a coroutine.
53
56
54
57
*limit * determines the buffer size limit used by the
55
- returned :class: `StreamReader ` instance.
58
+ returned :class: `StreamReader ` instance. By default the *limit *
59
+ is set to 64 KiB.
56
60
57
61
The rest of the arguments are passed directly to
58
62
:meth: `loop.create_connection `.
@@ -84,7 +88,8 @@ and work with streams:
84
88
automatically when this method is awaited from a coroutine.
85
89
86
90
*limit * determines the buffer size limit used by the
87
- returned :class: `StreamReader ` instance.
91
+ returned :class: `StreamReader ` instance. By default the *limit *
92
+ is set to 64 KiB.
88
93
89
94
The rest of the arguments are passed directly to
90
95
:meth: `loop.create_server `.
@@ -93,6 +98,9 @@ and work with streams:
93
98
94
99
The *ssl_handshake_timeout * and *start_serving * parameters.
95
100
101
+
102
+ .. rubric :: Unix Sockets
103
+
96
104
.. coroutinefunction :: open_unix_connection(path=None, \*, loop=None, \
97
105
limit=None, ssl=None, sock=None, \
98
106
server_hostname=None, ssl_handshake_timeout=None)
@@ -114,14 +122,15 @@ and work with streams:
114
122
115
123
The *path * parameter can now be a :term: `path-like object `
116
124
125
+
117
126
.. coroutinefunction :: start_unix_server(client_connected_cb, path=None, \
118
127
\* , loop=None, limit=None, sock=None, \
119
128
backlog=100, ssl=None, ssl_handshake_timeout=None, \
120
129
start_serving=True)
121
130
122
131
Start a UNIX socket server.
123
132
124
- Similar to :func: `start_server ` but operates on UNIX sockets.
133
+ Similar to :func: `start_server ` but works with UNIX sockets.
125
134
126
135
See also the documentation of :meth: `loop.create_unix_server `.
127
136
@@ -136,67 +145,47 @@ and work with streams:
136
145
The *path * parameter can now be a :term: `path-like object `.
137
146
138
147
139
- .. rubric :: Contents
140
-
141
- * `StreamReader `_ and `StreamWriter `_
142
- * `StreamReaderProtocol `_
143
- * `Examples `_
148
+ ---------
144
149
145
150
146
151
StreamReader
147
152
============
148
153
149
- .. class :: StreamReader(limit=_DEFAULT_LIMIT, loop=None)
150
-
151
- This class is :ref: `not thread safe <asyncio-multithreading >`.
152
-
153
- The *limit * argument's default value is set to _DEFAULT_LIMIT which is 2**16 (64 KiB)
154
-
155
- .. method :: exception()
156
-
157
- Get the exception.
158
-
159
- .. method :: feed_eof()
154
+ .. class :: StreamReader
160
155
161
- Acknowledge the EOF.
156
+ Represents a reader object that provides APIs to read data
157
+ from the IO stream.
162
158
163
- .. method :: feed_data(data)
164
-
165
- Feed *data * bytes in the internal buffer. Any operations waiting
166
- for the data will be resumed.
167
-
168
- .. method :: set_exception(exc)
169
-
170
- Set the exception.
171
-
172
- .. method :: set_transport(transport)
173
-
174
- Set the transport.
159
+ It is not recommended to instantiate *StreamReader * objects
160
+ directly; use :func: `open_connection ` and :func: `start_server `
161
+ instead.
175
162
176
163
.. coroutinemethod :: read(n=-1)
177
164
178
165
Read up to *n * bytes. If *n * is not provided, or set to ``-1 ``,
179
166
read until EOF and return all read bytes.
180
167
181
- If the EOF was received and the internal buffer is empty,
168
+ If an EOF was received and the internal buffer is empty,
182
169
return an empty ``bytes `` object.
183
170
184
171
.. coroutinemethod :: readline()
185
172
186
- Read one line, where "line" is a sequence of bytes ending with ``\n ``.
173
+ Read one line, where "line" is a sequence of bytes
174
+ ending with ``\n ``.
187
175
188
- If EOF is received, and ``\n `` was not found, the method will
189
- return the partial read bytes .
176
+ If an EOF is received and ``\n `` was not found, the method
177
+ returns partially read data .
190
178
191
- If the EOF was received and the internal buffer is empty,
179
+ If an EOF is received and the internal buffer is empty,
192
180
return an empty ``bytes `` object.
193
181
194
182
.. coroutinemethod :: readexactly(n)
195
183
196
- Read exactly *n * bytes. Raise an :exc: `IncompleteReadError ` if the end of
197
- the stream is reached before *n * can be read, the
198
- :attr: `IncompleteReadError.partial ` attribute of the exception contains
199
- the partial read bytes.
184
+ Read exactly *n * bytes.
185
+
186
+ Raise an :exc: `IncompleteReadError ` if an EOF reached before *n *
187
+ can be read. Use the :attr: `IncompleteReadError.partial `
188
+ attribute to get the partially read data.
200
189
201
190
.. coroutinemethod :: readuntil(separator=b'\\n')
202
191
@@ -231,105 +220,76 @@ StreamReader
231
220
StreamWriter
232
221
============
233
222
234
- .. class :: StreamWriter(transport, protocol, reader, loop)
223
+ .. class :: StreamWriter
235
224
236
- Wraps a Transport.
225
+ Represents a writer object that provides APIs to write data
226
+ to the IO stream.
237
227
238
- This exposes :meth: `write `, :meth: `writelines `, :meth: `can_write_eof() `,
239
- :meth: `write_eof `, :meth: `get_extra_info ` and :meth: `close `. It adds
240
- :meth: `drain ` which returns an optional :class: `Future ` on which you can
241
- wait for flow control. It also adds a transport attribute which references
242
- the :class: `Transport ` directly.
228
+ It is not recommended to instantiate *StreamWriter * objects
229
+ directly; use :func: `open_connection ` and :func: `start_server `
230
+ instead.
243
231
244
- This class is :ref: ` not thread safe < asyncio-multithreading >`.
232
+ .. method :: write(data)
245
233
246
- .. attribute :: transport
234
+ Write * data * to the stream.
247
235
248
- Transport.
236
+ .. method :: writelines(data)
249
237
250
- .. method :: can_write_eof()
238
+ Write a list (or any iterable) of bytes to the stream.
239
+
240
+ .. coroutinemethod :: drain()
241
+
242
+ Wait until it is appropriate to resume writing to the stream.
243
+ E.g.::
244
+
245
+ writer.write(data)
246
+ await writer.drain()
251
247
252
- Return :const: `True ` if the transport supports :meth: `write_eof `,
253
- :const: `False ` if not. See :meth: `WriteTransport.can_write_eof `.
248
+ This is a flow-control method that interacts with the underlying
249
+ IO write buffer. When the size of the buffer reaches
250
+ the high-water limit, *drain() * blocks until the size of the
251
+ buffer is drained down to the low-water limit and writing can
252
+ be resumed. When there is nothing to wait for, the :meth: `drain `
253
+ returns immediately.
254
254
255
255
.. method :: close()
256
256
257
- Close the transport: see :meth: ` BaseTransport.close ` .
257
+ Close the stream .
258
258
259
259
.. method :: is_closing()
260
260
261
- Return ``True `` if the writer is closing or is closed.
261
+ Return ``True `` if the stream is closed or in the process of
262
+ being closed.
262
263
263
264
.. versionadded :: 3.7
264
265
265
266
.. coroutinemethod :: wait_closed()
266
267
267
- Wait until the writer is closed.
268
+ Wait until the stream is closed.
268
269
269
- Should be called after :meth: `close ` to wait until the underlying
270
- connection (and the associated transport/protocol pair) is closed.
270
+ Should be called after :meth: `close ` to wait until the underlying
271
+ connection is closed.
271
272
272
273
.. versionadded :: 3.7
273
274
274
- .. coroutinemethod :: drain()
275
-
276
- Let the write buffer of the underlying transport a chance to be flushed.
277
-
278
- The intended use is to write::
279
-
280
- w.write(data)
281
- await w.drain()
282
-
283
- When the size of the transport buffer reaches the high-water limit (the
284
- protocol is paused), block until the size of the buffer is drained down
285
- to the low-water limit and the protocol is resumed. When there is nothing
286
- to wait for, the yield-from continues immediately.
287
-
288
- Yielding from :meth: `drain ` gives the opportunity for the loop to
289
- schedule the write operation and flush the buffer. It should especially
290
- be used when a possibly large amount of data is written to the transport,
291
- and the coroutine does not yield-from between calls to :meth: `write `.
292
-
293
- This method is a :ref: `coroutine <coroutine >`.
294
-
295
- .. method :: get_extra_info(name, default=None)
296
-
297
- Return optional transport information: see
298
- :meth: `BaseTransport.get_extra_info `.
299
-
300
- .. method :: write(data)
301
-
302
- Write some *data * bytes to the transport: see
303
- :meth: `WriteTransport.write `.
304
-
305
- .. method :: writelines(data)
275
+ .. method :: can_write_eof()
306
276
307
- Write a list (or any iterable) of data bytes to the transport:
308
- see :meth: `WriteTransport.writelines ` .
277
+ Return * True * if the underlying transport supports
278
+ the :meth: `write_eof ` method, * False * otherwise .
309
279
310
280
.. method :: write_eof()
311
281
312
- Close the write end of the transport after flushing buffered data:
313
- see :meth: `WriteTransport.write_eof `.
314
-
315
-
316
- StreamReaderProtocol
317
- ====================
282
+ Close the write end of the stream after the buffered write
283
+ data is flushed.
318
284
319
- .. class :: StreamReaderProtocol(stream_reader, client_connected_cb=None, \
320
- loop=None)
285
+ .. attribute :: transport
321
286
322
- Trivial helper class to adapt between :class: `Protocol ` and
323
- :class: `StreamReader `. Subclass of :class: `Protocol `.
287
+ Return the underlying asyncio transport.
324
288
325
- *stream_reader * is a :class: `StreamReader ` instance, *client_connected_cb *
326
- is an optional function called with (stream_reader, stream_writer) when a
327
- connection is made, *loop * is the event loop instance to use.
289
+ .. method :: get_extra_info(name, default=None)
328
290
329
- (This is a helper class instead of making :class: `StreamReader ` itself a
330
- :class: `Protocol ` subclass, because the :class: `StreamReader ` has other
331
- potential uses, and to prevent the user of the :class: `StreamReader ` from
332
- accidentally calling inappropriate methods of the protocol.)
291
+ Access optional transport information; see
292
+ :meth: `BaseTransport.get_extra_info ` for details.
333
293
334
294
335
295
Examples
0 commit comments