diff options
author | Yury Selivanov <yury@magic.io> | 2018-01-25 23:08:09 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-25 23:08:09 (GMT) |
commit | c9070d03f5169ad6e171e641b7fa8feab18bf229 (patch) | |
tree | 4bad875e6f68874a980e5289a45893f4335c5afb /Lib/asyncio/events.py | |
parent | 1aa094f74039cd20fdc7df56c68f6848c18ce4dd (diff) | |
download | cpython-c9070d03f5169ad6e171e641b7fa8feab18bf229.zip cpython-c9070d03f5169ad6e171e641b7fa8feab18bf229.tar.gz cpython-c9070d03f5169ad6e171e641b7fa8feab18bf229.tar.bz2 |
bpo-32662: Implement Server.start_serving() and Server.serve_forever() (#5312)
* bpo-32662: Implement Server.start_serving() and Server.serve_forever()
New methods:
* Server.start_serving(),
* Server.serve_forever(), and
* Server.is_serving().
Add 'start_serving' keyword parameter to loop.create_server() and
loop.create_unix_server().
Diffstat (limited to 'Lib/asyncio/events.py')
-rw-r--r-- | Lib/asyncio/events.py | 48 |
1 files changed, 43 insertions, 5 deletions
diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py index 5c68d4c..7aa3de0 100644 --- a/Lib/asyncio/events.py +++ b/Lib/asyncio/events.py @@ -164,13 +164,39 @@ class AbstractServer: """Stop serving. This leaves existing connections open.""" raise NotImplementedError + def get_loop(self): + """Get the event loop the Server object is attached to.""" + raise NotImplementedError + + def is_serving(self): + """Return True if the server is accepting connections.""" + raise NotImplementedError + + async def start_serving(self): + """Start accepting connections. + + This method is idempotent, so it can be called when + the server is already being serving. + """ + raise NotImplementedError + + async def serve_forever(self): + """Start accepting connections until the coroutine is cancelled. + + The server is closed when the coroutine is cancelled. + """ + raise NotImplementedError + async def wait_closed(self): """Coroutine to wait until service is closed.""" raise NotImplementedError - def get_loop(self): - """ Get the event loop the Server object is attached to.""" - raise NotImplementedError + async def __aenter__(self): + return self + + async def __aexit__(self, *exc): + self.close() + await self.wait_closed() class AbstractEventLoop: @@ -279,7 +305,8 @@ class AbstractEventLoop: *, family=socket.AF_UNSPEC, flags=socket.AI_PASSIVE, sock=None, backlog=100, ssl=None, reuse_address=None, reuse_port=None, - ssl_handshake_timeout=None): + ssl_handshake_timeout=None, + start_serving=True): """A coroutine which creates a TCP server bound to host and port. The return value is a Server object which can be used to stop @@ -319,6 +346,11 @@ class AbstractEventLoop: will wait for completion of the SSL handshake before aborting the connection. Default is 10s, longer timeouts may increase vulnerability to DoS attacks (see https://support.f5.com/csp/article/K13834) + + start_serving set to True (default) causes the created server + to start accepting connections immediately. When set to False, + the user should await Server.start_serving() or Server.serve_forever() + to make the server to start accepting connections. """ raise NotImplementedError @@ -343,7 +375,8 @@ class AbstractEventLoop: async def create_unix_server( self, protocol_factory, path=None, *, sock=None, backlog=100, ssl=None, - ssl_handshake_timeout=None): + ssl_handshake_timeout=None, + start_serving=True): """A coroutine which creates a UNIX Domain Socket server. The return value is a Server object, which can be used to stop @@ -363,6 +396,11 @@ class AbstractEventLoop: ssl_handshake_timeout is the time in seconds that an SSL server will wait for the SSL handshake to complete (defaults to 10s). + + start_serving set to True (default) causes the created server + to start accepting connections immediately. When set to False, + the user should await Server.start_serving() or Server.serve_forever() + to make the server to start accepting connections. """ raise NotImplementedError |