Skip to content

Commit be49063

Browse files
committed
asyncio: document network functions, and stream reader/writer
1 parent f217206 commit be49063

1 file changed

Lines changed: 152 additions & 1 deletion

File tree

Doc/library/asyncio.rst

Lines changed: 152 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ Run an event loop
9494

9595
.. method:: BaseEventLoop.run_until_complete(future)
9696

97-
Run until the :class:`Future` is done.
97+
Run until the :class:`~concurrent.futures.Future` is done.
9898

9999
If the argument is a coroutine, it is wrapped in a :class:`Task`.
100100

@@ -336,6 +336,58 @@ Run subprocesses asynchronously using the :mod:`subprocess` module.
336336
See the constructor of the :class:`subprocess.Popen` class for parameters.
337337

338338

339+
Network functions
340+
-----------------
341+
342+
.. function:: open_connection(host=None, port=None, *, loop=None, limit=_DEFAULT_LIMIT, **kwds)
343+
344+
A wrapper for create_connection() returning a (reader, writer) pair.
345+
346+
The reader returned is a StreamReader instance; the writer is a
347+
:class:`Transport`.
348+
349+
The arguments are all the usual arguments to
350+
:meth:`BaseEventLoop.create_connection` except *protocol_factory*; most
351+
common are positional host and port, with various optional keyword arguments
352+
following.
353+
354+
Additional optional keyword arguments are *loop* (to set the event loop
355+
instance to use) and *limit* (to set the buffer limit passed to the
356+
StreamReader).
357+
358+
(If you want to customize the :class:`StreamReader` and/or
359+
:class:`StreamReaderProtocol` classes, just copy the code -- there's really
360+
nothing special here except some convenience.)
361+
362+
This function returns a :ref:`coroutine <coroutine>`.
363+
364+
.. function:: start_server(client_connected_cb, host=None, port=None, *, loop=None, limit=_DEFAULT_LIMIT, **kwds)
365+
366+
Start a socket server, call back for each client connected.
367+
368+
The first parameter, *client_connected_cb*, takes two parameters:
369+
*client_reader*, *client_writer*. *client_reader* is a
370+
:class:`StreamReader` object, while *client_writer* is a
371+
:class:`StreamWriter` object. This parameter can either be a plain callback
372+
function or a :ref:`coroutine <coroutine>`; if it is a coroutine, it will be
373+
automatically converted into a :class:`Task`.
374+
375+
The rest of the arguments are all the usual arguments to
376+
:meth:`~BaseEventLoop.create_server()` except *protocol_factory*; most
377+
common are positional host and port, with various optional keyword arguments
378+
following. The return value is the same as
379+
:meth:`~BaseEventLoop.create_server()`.
380+
381+
Additional optional keyword arguments are *loop* (to set the event loop
382+
instance to use) and *limit* (to set the buffer limit passed to the
383+
:class:`StreamReader`).
384+
385+
The return value is the same as :meth:`~BaseEventLoop.create_server()`, i.e.
386+
a Server object which can be used to stop the service.
387+
388+
This function returns a :ref:`coroutine <coroutine>`.
389+
390+
339391
.. _protocol:
340392

341393
Protocols
@@ -843,6 +895,105 @@ call them yourself, unless you are implementing a transport.
843895
are interested.
844896

845897

898+
Stream reader and writer
899+
------------------------
900+
901+
.. class:: StreamWriter(transport, protocol, reader, loop)
902+
903+
Wraps a Transport.
904+
905+
This exposes :meth:`write`, :meth:`writelines`, :meth:`can_write_eof()`, :meth:`write_eof`, :meth:`get_extra_info` and
906+
:meth:`close`. It adds :meth:`drain` which returns an optional :class:`~concurrent.futures.Future` on which you can
907+
wait for flow control. It also adds a transport attribute which references
908+
the :class:`Transport` directly.
909+
910+
.. attribute:: transport
911+
912+
Transport.
913+
914+
.. method:: write(data)
915+
916+
XXX
917+
918+
.. method:: writelines(data)
919+
920+
XXX
921+
922+
.. method:: write_eof()
923+
924+
XXX
925+
926+
.. method:: can_write_eof()
927+
928+
XXX
929+
930+
.. method:: close()
931+
932+
XXX
933+
934+
.. method:: get_extra_info(name, default=None)
935+
936+
XXX
937+
938+
.. method:: drain()
939+
940+
This method has an unusual return value.
941+
942+
The intended use is to write::
943+
944+
w.write(data)
945+
yield from w.drain()
946+
947+
When there's nothing to wait for, :meth:`drain()` returns ``()``, and the
948+
yield-from continues immediately. When the transport buffer is full (the
949+
protocol is paused), :meth:`drain` creates and returns a
950+
:class:`~concurrent.futures.Future` and the yield-from will block until
951+
that Future is completed, which will happen when the buffer is
952+
(partially) drained and the protocol is resumed.
953+
954+
955+
.. class:: StreamReader(limit=_DEFAULT_LIMIT, loop=None)
956+
957+
.. method:: exception()
958+
959+
Get the exception.
960+
961+
.. method:: set_exception(exc)
962+
963+
Set the exception.
964+
965+
.. method:: set_transport(transport)
966+
967+
Set the transport.
968+
969+
.. method:: feed_eof()
970+
971+
XXX
972+
973+
.. method:: feed_data(data)
974+
975+
XXX
976+
977+
.. method:: read(n=-1)
978+
979+
XXX
980+
981+
This method returns a :ref:`coroutine <coroutine>`.
982+
983+
.. method:: readexactly(n)
984+
985+
XXX
986+
987+
This method returns a :ref:`coroutine <coroutine>`.
988+
989+
.. method:: readline()
990+
991+
XXX
992+
993+
This method returns a :ref:`coroutine <coroutine>`.
994+
995+
996+
846997
.. _coroutine:
847998

848999
Coroutines

0 commit comments

Comments
 (0)