Skip to content

Commit b10d23a

Browse files
committed
asyncio: improve the documentation of servers
- Fix the documentation of Server.close(): it closes sockets - Replace AbstractServer with Server - Document Server.sockets attribute
1 parent bbff83d commit b10d23a

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

Doc/library/asyncio-eventloop.rst

+21-6
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,9 @@ Creating listening connections
263263

264264
.. method:: BaseEventLoop.create_server(protocol_factory, host=None, port=None, \*, family=socket.AF_UNSPEC, flags=socket.AI_PASSIVE, sock=None, backlog=100, ssl=None, reuse_address=None)
265265

266-
Create a TCP server bound to host and port. Return an
267-
:class:`AbstractServer` object which can be used to stop the service.
266+
Create a TCP server bound to host and port. Return a :class:`Server` object,
267+
its :attr:`~Server.sockets` attribute contains created sockets. Use the
268+
:meth:`Server.close` method to stop the server: close listening sockets.
268269

269270
This method is a :ref:`coroutine <coroutine>`.
270271

@@ -557,17 +558,31 @@ Debug mode
557558
Server
558559
------
559560

560-
.. class:: AbstractServer
561+
.. class:: Server
561562

562-
Abstract server returned by :func:`BaseEventLoop.create_server`.
563+
Server listening on sockets.
564+
565+
Object created by the :meth:`BaseEventLoop.create_server` method and the
566+
:func:`start_server` function. Don't instanciate the class directly.
563567

564568
.. method:: close()
565569

566-
Stop serving. This leaves existing connections open.
570+
Stop serving: close all sockets and set the :attr:`sockets` attribute to
571+
``None``.
572+
573+
The server is closed asynchonously, use the :meth:`wait_closed` coroutine
574+
to wait until the server is closed.
567575

568576
.. method:: wait_closed()
569577

570-
A :ref:`coroutine <coroutine>` to wait until service is closed.
578+
Wait until the :meth:`close` method completes.
579+
580+
This method is a :ref:`coroutine <coroutine>`.
581+
582+
.. attribute:: sockets
583+
584+
List of :class:`socket.socket` objects the server is listening to, or
585+
``None`` if the server is closed.
571586

572587

573588
Handle

Doc/library/asyncio-stream.rst

+9-12
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,26 @@ Stream functions
3434

3535
.. function:: start_server(client_connected_cb, host=None, port=None, \*, loop=None, limit=None, **kwds)
3636

37-
Start a socket server, with a callback for each client connected.
37+
Start a socket server, with a callback for each client connected. The return
38+
value is the same as :meth:`~BaseEventLoop.create_server()`.
3839

39-
The first parameter, *client_connected_cb*, takes two parameters:
40+
The *client_connected_cb* parameter is called with two parameters:
4041
*client_reader*, *client_writer*. *client_reader* is a
4142
:class:`StreamReader` object, while *client_writer* is a
42-
:class:`StreamWriter` object. This parameter can either be a plain callback
43-
function or a :ref:`coroutine function <coroutine>`; if it is a coroutine
44-
function, it will be automatically wrapped in a future using the
45-
:meth:`BaseEventLoop.create_task` method.
43+
:class:`StreamWriter` object. The *client_connected_cb* parameter can
44+
either be a plain callback function or a :ref:`coroutine function
45+
<coroutine>`; if it is a coroutine function, it will be automatically
46+
wrapped in a future using the :meth:`BaseEventLoop.create_task` method.
4647

4748
The rest of the arguments are all the usual arguments to
4849
:meth:`~BaseEventLoop.create_server()` except *protocol_factory*; most
49-
common are positional host and port, with various optional keyword arguments
50-
following. The return value is the same as
51-
:meth:`~BaseEventLoop.create_server()`.
50+
common are positional *host* and *port*, with various optional keyword
51+
arguments following.
5252

5353
Additional optional keyword arguments are *loop* (to set the event loop
5454
instance to use) and *limit* (to set the buffer limit passed to the
5555
:class:`StreamReader`).
5656

57-
The return value is the same as :meth:`~BaseEventLoop.create_server()`, i.e.
58-
a :class:`AbstractServer` object which can be used to stop the service.
59-
6057
This function is a :ref:`coroutine <coroutine>`.
6158

6259
.. function:: open_unix_connection(path=None, \*, loop=None, limit=None, **kwds)

Lib/asyncio/base_events.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ def close(self):
110110
return
111111
self.sockets = None
112112
for sock in sockets:
113+
# closing sockets will call asynchronously the _detach() method
114+
# which calls _wakeup() for the last socket
113115
self._loop._stop_serving(sock)
114116
if self._active_count == 0:
115117
self._wakeup()
@@ -626,7 +628,7 @@ def create_server(self, protocol_factory, host=None, port=None,
626628
reuse_address=None):
627629
"""Create a TCP server bound to host and port.
628630
629-
Return an AbstractServer object which can be used to stop the service.
631+
Return an Server object which can be used to stop the service.
630632
631633
This method is a coroutine.
632634
"""

0 commit comments

Comments
 (0)