Skip to content

Commit 0ce601f

Browse files
committed
Merge 3.4
2 parents 85024cc + b10d23a commit 0ce601f

File tree

5 files changed

+58
-43
lines changed

5 files changed

+58
-43
lines changed

Doc/library/asyncio-eventloop.rst

Lines changed: 21 additions & 6 deletions
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

Lines changed: 9 additions & 12 deletions
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

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -89,43 +89,46 @@ def _raise_stop_error(*args):
8989
class Server(events.AbstractServer):
9090

9191
def __init__(self, loop, sockets):
92-
self.loop = loop
92+
self._loop = loop
9393
self.sockets = sockets
94-
self.active_count = 0
95-
self.waiters = []
94+
self._active_count = 0
95+
self._waiters = []
9696

97-
def attach(self, transport):
97+
def _attach(self):
9898
assert self.sockets is not None
99-
self.active_count += 1
99+
self._active_count += 1
100100

101-
def detach(self, transport):
102-
assert self.active_count > 0
103-
self.active_count -= 1
104-
if self.active_count == 0 and self.sockets is None:
101+
def _detach(self):
102+
assert self._active_count > 0
103+
self._active_count -= 1
104+
if self._active_count == 0 and self.sockets is None:
105105
self._wakeup()
106106

107107
def close(self):
108108
sockets = self.sockets
109-
if sockets is not None:
110-
self.sockets = None
111-
for sock in sockets:
112-
self.loop._stop_serving(sock)
113-
if self.active_count == 0:
114-
self._wakeup()
109+
if sockets is None:
110+
return
111+
self.sockets = None
112+
for sock in sockets:
113+
# closing sockets will call asynchronously the _detach() method
114+
# which calls _wakeup() for the last socket
115+
self._loop._stop_serving(sock)
116+
if self._active_count == 0:
117+
self._wakeup()
115118

116119
def _wakeup(self):
117-
waiters = self.waiters
118-
self.waiters = None
120+
waiters = self._waiters
121+
self._waiters = None
119122
for waiter in waiters:
120123
if not waiter.done():
121124
waiter.set_result(waiter)
122125

123126
@coroutine
124127
def wait_closed(self):
125-
if self.sockets is None or self.waiters is None:
128+
if self.sockets is None or self._waiters is None:
126129
return
127-
waiter = futures.Future(loop=self.loop)
128-
self.waiters.append(waiter)
130+
waiter = futures.Future(loop=self._loop)
131+
self._waiters.append(waiter)
129132
yield from waiter
130133

131134

@@ -625,7 +628,7 @@ def create_server(self, protocol_factory, host=None, port=None,
625628
reuse_address=None):
626629
"""Create a TCP server bound to host and port.
627630
628-
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.
629632
630633
This method is a coroutine.
631634
"""

Lib/asyncio/proactor_events.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __init__(self, loop, sock, protocol, waiter=None,
3535
self._closing = False # Set when close() called.
3636
self._eof_written = False
3737
if self._server is not None:
38-
self._server.attach(self)
38+
self._server._attach()
3939
self._loop.call_soon(self._protocol.connection_made, self)
4040
if waiter is not None:
4141
# wait until protocol.connection_made() has been called
@@ -91,7 +91,7 @@ def _call_connection_lost(self, exc):
9191
self._sock.close()
9292
server = self._server
9393
if server is not None:
94-
server.detach(self)
94+
server._detach()
9595
self._server = None
9696

9797
def get_write_buffer_size(self):

Lib/asyncio/selector_events.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ def __init__(self, loop, sock, protocol, extra, server=None):
417417
self._conn_lost = 0 # Set when call to connection_lost scheduled.
418418
self._closing = False # Set when close() called.
419419
if self._server is not None:
420-
self._server.attach(self)
420+
self._server._attach()
421421

422422
def abort(self):
423423
self._force_close(None)
@@ -464,7 +464,7 @@ def _call_connection_lost(self, exc):
464464
self._loop = None
465465
server = self._server
466466
if server is not None:
467-
server.detach(self)
467+
server._detach()
468468
self._server = None
469469

470470
def get_write_buffer_size(self):

0 commit comments

Comments
 (0)