Skip to content

Commit 8be876e

Browse files
authored
bpo-33649: Cleanup asyncio/streams and asyncio/synchronization docs (GH-9192)
1 parent fa7dfae commit 8be876e

File tree

3 files changed

+263
-286
lines changed

3 files changed

+263
-286
lines changed

Doc/library/asyncio-stream.rst

Lines changed: 73 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ Streams
77
=======
88

99
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
1111
using callbacks or low-level protocols and transports.
1212

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
1414
streams::
1515

1616
import asyncio
@@ -31,6 +31,9 @@ streams::
3131
asyncio.run(tcp_echo_client('Hello World!'))
3232

3333

34+
See also the `Examples`_ section below.
35+
36+
3437
.. rubric:: Stream Functions
3538

3639
The following top-level asyncio functions can be used to create
@@ -43,7 +46,7 @@ and work with streams:
4346
server_hostname=None, ssl_handshake_timeout=None)
4447

4548
Establish a network connection and return a pair of
46-
``(reader, writer)``.
49+
``(reader, writer)`` objects.
4750

4851
The returned *reader* and *writer* objects are instances of
4952
:class:`StreamReader` and :class:`StreamWriter` classes.
@@ -52,7 +55,8 @@ and work with streams:
5255
automatically when this method is awaited from a coroutine.
5356

5457
*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.
5660

5761
The rest of the arguments are passed directly to
5862
:meth:`loop.create_connection`.
@@ -84,7 +88,8 @@ and work with streams:
8488
automatically when this method is awaited from a coroutine.
8589

8690
*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.
8893

8994
The rest of the arguments are passed directly to
9095
:meth:`loop.create_server`.
@@ -93,6 +98,9 @@ and work with streams:
9398

9499
The *ssl_handshake_timeout* and *start_serving* parameters.
95100

101+
102+
.. rubric:: Unix Sockets
103+
96104
.. coroutinefunction:: open_unix_connection(path=None, \*, loop=None, \
97105
limit=None, ssl=None, sock=None, \
98106
server_hostname=None, ssl_handshake_timeout=None)
@@ -114,14 +122,15 @@ and work with streams:
114122

115123
The *path* parameter can now be a :term:`path-like object`
116124

125+
117126
.. coroutinefunction:: start_unix_server(client_connected_cb, path=None, \
118127
\*, loop=None, limit=None, sock=None, \
119128
backlog=100, ssl=None, ssl_handshake_timeout=None, \
120129
start_serving=True)
121130

122131
Start a UNIX socket server.
123132

124-
Similar to :func:`start_server` but operates on UNIX sockets.
133+
Similar to :func:`start_server` but works with UNIX sockets.
125134

126135
See also the documentation of :meth:`loop.create_unix_server`.
127136

@@ -136,67 +145,47 @@ and work with streams:
136145
The *path* parameter can now be a :term:`path-like object`.
137146

138147

139-
.. rubric:: Contents
140-
141-
* `StreamReader`_ and `StreamWriter`_
142-
* `StreamReaderProtocol`_
143-
* `Examples`_
148+
---------
144149

145150

146151
StreamReader
147152
============
148153

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
160155

161-
Acknowledge the EOF.
156+
Represents a reader object that provides APIs to read data
157+
from the IO stream.
162158

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.
175162

176163
.. coroutinemethod:: read(n=-1)
177164

178165
Read up to *n* bytes. If *n* is not provided, or set to ``-1``,
179166
read until EOF and return all read bytes.
180167

181-
If the EOF was received and the internal buffer is empty,
168+
If an EOF was received and the internal buffer is empty,
182169
return an empty ``bytes`` object.
183170

184171
.. coroutinemethod:: readline()
185172

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``.
187175

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.
190178

191-
If the EOF was received and the internal buffer is empty,
179+
If an EOF is received and the internal buffer is empty,
192180
return an empty ``bytes`` object.
193181

194182
.. coroutinemethod:: readexactly(n)
195183

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.
200189

201190
.. coroutinemethod:: readuntil(separator=b'\\n')
202191

@@ -231,105 +220,76 @@ StreamReader
231220
StreamWriter
232221
============
233222

234-
.. class:: StreamWriter(transport, protocol, reader, loop)
223+
.. class:: StreamWriter
235224

236-
Wraps a Transport.
225+
Represents a writer object that provides APIs to write data
226+
to the IO stream.
237227

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.
243231

244-
This class is :ref:`not thread safe <asyncio-multithreading>`.
232+
.. method:: write(data)
245233

246-
.. attribute:: transport
234+
Write *data* to the stream.
247235

248-
Transport.
236+
.. method:: writelines(data)
249237

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()
251247

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.
254254

255255
.. method:: close()
256256

257-
Close the transport: see :meth:`BaseTransport.close`.
257+
Close the stream.
258258

259259
.. method:: is_closing()
260260

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.
262263

263264
.. versionadded:: 3.7
264265

265266
.. coroutinemethod:: wait_closed()
266267

267-
Wait until the writer is closed.
268+
Wait until the stream is closed.
268269

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.
271272

272273
.. versionadded:: 3.7
273274

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()
306276

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.
309279

310280
.. method:: write_eof()
311281

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.
318284

319-
.. class:: StreamReaderProtocol(stream_reader, client_connected_cb=None, \
320-
loop=None)
285+
.. attribute:: transport
321286

322-
Trivial helper class to adapt between :class:`Protocol` and
323-
:class:`StreamReader`. Subclass of :class:`Protocol`.
287+
Return the underlying asyncio transport.
324288

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)
328290

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.
333293

334294

335295
Examples

0 commit comments

Comments
 (0)