summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbeavailable <beavailable@proton.me>2023-12-13 03:23:29 (GMT)
committerGitHub <noreply@github.com>2023-12-13 03:23:29 (GMT)
commit3aea6c4823e90172c9bc36cd20dc51b295d8a3c4 (patch)
tree12040ee3f1f6e9fcb679b8be79905587281592ee
parenta3a1cb48456c809f7b1ab6a6ffe83e8b3f69be0f (diff)
downloadcpython-3aea6c4823e90172c9bc36cd20dc51b295d8a3c4.zip
cpython-3aea6c4823e90172c9bc36cd20dc51b295d8a3c4.tar.gz
cpython-3aea6c4823e90172c9bc36cd20dc51b295d8a3c4.tar.bz2
gh-101336: Add keep_alive keyword arg for asyncio create_server() (#112485)
-rw-r--r--Doc/library/asyncio-eventloop.rst8
-rw-r--r--Lib/asyncio/base_events.py4
-rw-r--r--Lib/asyncio/events.py4
-rw-r--r--Misc/NEWS.d/next/Library/2023-11-28-02-39-30.gh-issue-101336.ya433z.rst1
4 files changed, 17 insertions, 0 deletions
diff --git a/Doc/library/asyncio-eventloop.rst b/Doc/library/asyncio-eventloop.rst
index ea1d146..828e506 100644
--- a/Doc/library/asyncio-eventloop.rst
+++ b/Doc/library/asyncio-eventloop.rst
@@ -671,6 +671,7 @@ Creating network servers
flags=socket.AI_PASSIVE, \
sock=None, backlog=100, ssl=None, \
reuse_address=None, reuse_port=None, \
+ keep_alive=None, \
ssl_handshake_timeout=None, \
ssl_shutdown_timeout=None, \
start_serving=True)
@@ -735,6 +736,13 @@ Creating network servers
set this flag when being created. This option is not supported on
Windows.
+ * *keep_alive* set to ``True`` keeps connections active by enabling the
+ periodic transmission of messages.
+
+ .. versionchanged:: 3.13
+
+ Added the *keep_alive* parameter.
+
* *ssl_handshake_timeout* is (for a TLS server) the time in seconds to wait
for the TLS handshake to complete before aborting the connection.
``60.0`` seconds if ``None`` (default).
diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py
index 416c732..a8870b6 100644
--- a/Lib/asyncio/base_events.py
+++ b/Lib/asyncio/base_events.py
@@ -1496,6 +1496,7 @@ class BaseEventLoop(events.AbstractEventLoop):
ssl=None,
reuse_address=None,
reuse_port=None,
+ keep_alive=None,
ssl_handshake_timeout=None,
ssl_shutdown_timeout=None,
start_serving=True):
@@ -1569,6 +1570,9 @@ class BaseEventLoop(events.AbstractEventLoop):
socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
if reuse_port:
_set_reuseport(sock)
+ if keep_alive:
+ sock.setsockopt(
+ socket.SOL_SOCKET, socket.SO_KEEPALIVE, True)
# Disable IPv4/IPv6 dual stack support (enabled by
# default on Linux) which makes a single socket
# listen on both address families.
diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py
index 0ccf851..ebc3836 100644
--- a/Lib/asyncio/events.py
+++ b/Lib/asyncio/events.py
@@ -316,6 +316,7 @@ class AbstractEventLoop:
*, family=socket.AF_UNSPEC,
flags=socket.AI_PASSIVE, sock=None, backlog=100,
ssl=None, reuse_address=None, reuse_port=None,
+ keep_alive=None,
ssl_handshake_timeout=None,
ssl_shutdown_timeout=None,
start_serving=True):
@@ -354,6 +355,9 @@ class AbstractEventLoop:
they all set this flag when being created. This option is not
supported on Windows.
+ keep_alive set to True keeps connections active by enabling the
+ periodic transmission of messages.
+
ssl_handshake_timeout is the time in seconds that an SSL server
will wait for completion of the SSL handshake before aborting the
connection. Default is 60s.
diff --git a/Misc/NEWS.d/next/Library/2023-11-28-02-39-30.gh-issue-101336.ya433z.rst b/Misc/NEWS.d/next/Library/2023-11-28-02-39-30.gh-issue-101336.ya433z.rst
new file mode 100644
index 0000000..c222feb
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-11-28-02-39-30.gh-issue-101336.ya433z.rst
@@ -0,0 +1 @@
+Add ``keep_alive`` keyword parameter for :meth:`AbstractEventLoop.create_server` and :meth:`BaseEventLoop.create_server`.