[Python-checkins] bpo-33540: Add block_on_close attr to socketserver (GH-6911)

Miss Islington (bot) webhook-mailer at python.org
Wed May 23 21:34:46 EDT 2018


https://github.com/python/cpython/commit/fa286edbde9ed660d99628aea14ee3b824c2afe6
commit: fa286edbde9ed660d99628aea14ee3b824c2afe6
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-05-23T18:34:43-07:00
summary:

bpo-33540: Add block_on_close attr to socketserver (GH-6911)


Add a new block_on_close class attribute to ForkingMixIn and
ThreadingMixIn classes of socketserver to opt-in for pre-3.7 behaviour.
(cherry picked from commit 453bd0bc65b7ea6a18c43da69143ab10d54c0a35)

Co-authored-by: Victor Stinner <vstinner at redhat.com>

files:
A Misc/NEWS.d/next/Library/2018-05-16-18-10-38.bpo-33540.wy9LRV.rst
M Doc/library/socketserver.rst
M Doc/whatsnew/3.7.rst
M Lib/socketserver.py

diff --git a/Doc/library/socketserver.rst b/Doc/library/socketserver.rst
index 560002738375..1b3062da6df9 100644
--- a/Doc/library/socketserver.rst
+++ b/Doc/library/socketserver.rst
@@ -116,10 +116,13 @@ server classes.
    only available on POSIX platforms that support :func:`~os.fork`.
 
    :meth:`socketserver.ForkingMixIn.server_close` waits until all child
-   processes complete.
+   processes complete, except if
+   :attr:`socketserver.ForkingMixIn.block_on_close` attribute is false.
 
    :meth:`socketserver.ThreadingMixIn.server_close` waits until all non-daemon
-   threads complete. Use daemonic threads by setting
+   threads complete, except if
+   :attr:`socketserver.ThreadingMixIn.block_on_close` attribute is false. Use
+   daemonic threads by setting
    :data:`ThreadingMixIn.daemon_threads` to ``True`` to not wait until threads
    complete.
 
@@ -128,6 +131,8 @@ server classes.
       :meth:`socketserver.ForkingMixIn.server_close` and
       :meth:`socketserver.ThreadingMixIn.server_close` now waits until all
       child processes and non-daemonic threads complete.
+      Add a new :attr:`socketserver.ForkingMixIn.block_on_close` class
+      attribute to opt-in for the pre-3.7 behaviour.
 
 
 .. class:: ForkingTCPServer
diff --git a/Doc/whatsnew/3.7.rst b/Doc/whatsnew/3.7.rst
index 46015af3e739..b466e74cffb4 100644
--- a/Doc/whatsnew/3.7.rst
+++ b/Doc/whatsnew/3.7.rst
@@ -1216,6 +1216,18 @@ by default.
 (Contributed by Christian Heimes in :issue:`28134`.)
 
 
+socketserver
+------------
+
+:meth:`socketserver.ThreadingMixIn.server_close` now waits until all non-daemon
+threads complete. :meth:`socketserver.ForkingMixIn.server_close` now waits
+until all child processes complete.
+
+Add a new :attr:`socketserver.ForkingMixIn.block_on_close` class attribute to
+:class:`socketserver.ForkingMixIn` and :class:`socketserver.ThreadingMixIn`
+classes. Set the class attribute to ``False`` to get the pre-3.7 behaviour.
+
+
 sqlite3
 -------
 
@@ -2156,10 +2168,17 @@ Changes in the Python API
   and module are affected by this change. (Contributed by INADA Naoki and
   Eugene Toder in :issue:`29463`.)
 
-* :meth:`~socketserver.BaseServer.server_close` in
-  :class:`socketserver.ThreadingMixIn` and :class:`socketserver.ForkingMixIn`
-  now waits until all non-daemon threads complete.
-  (Contributed by Victor Stinner in :issue:`31233` and :issue:`31151`.)
+* :meth:`socketserver.ThreadingMixIn.server_close` now waits until all
+  non-daemon threads complete.  Set the new
+  :attr:`socketserver.ThreadingMixIn.block_on_close` class attribute to
+  ``False`` to get the pre-3.7 behaviour.
+  (Contributed by Victor Stinner in :issue:`31233` and :issue:`33540`.)
+
+* :meth:`socketserver.ForkingMixIn.server_close` now waits until all
+  child processes complete. Set the new
+  :attr:`socketserver.ForkingMixIn.block_on_close` class attribute to ``False``
+  to get the pre-3.7 behaviour.
+  (Contributed by Victor Stinner in :issue:`31151` and :issue:`33540`.)
 
 * The :func:`locale.localeconv` function now temporarily sets the ``LC_CTYPE``
   locale to the value of ``LC_NUMERIC`` in some cases.
diff --git a/Lib/socketserver.py b/Lib/socketserver.py
index 1ae7bef90407..71bb9a48fa91 100644
--- a/Lib/socketserver.py
+++ b/Lib/socketserver.py
@@ -543,6 +543,8 @@ class ForkingMixIn:
         timeout = 300
         active_children = None
         max_children = 40
+        # If true, server_close() waits until all child processes complete.
+        block_on_close = True
 
         def collect_children(self, *, blocking=False):
             """Internal routine to wait for children that have exited."""
@@ -620,7 +622,7 @@ def process_request(self, request, client_address):
 
         def server_close(self):
             super().server_close()
-            self.collect_children(blocking=True)
+            self.collect_children(blocking=self.block_on_close)
 
 
 class ThreadingMixIn:
@@ -629,6 +631,8 @@ class ThreadingMixIn:
     # Decides how threads will act upon termination of the
     # main process
     daemon_threads = False
+    # If true, server_close() waits until all non-daemonic threads terminate.
+    block_on_close = True
     # For non-daemonic threads, list of threading.Threading objects
     # used by server_close() to wait for all threads completion.
     _threads = None
@@ -659,11 +663,12 @@ def process_request(self, request, client_address):
 
     def server_close(self):
         super().server_close()
-        threads = self._threads
-        self._threads = None
-        if threads:
-            for thread in threads:
-                thread.join()
+        if self.block_on_close:
+            threads = self._threads
+            self._threads = None
+            if threads:
+                for thread in threads:
+                    thread.join()
 
 
 if hasattr(os, "fork"):
diff --git a/Misc/NEWS.d/next/Library/2018-05-16-18-10-38.bpo-33540.wy9LRV.rst b/Misc/NEWS.d/next/Library/2018-05-16-18-10-38.bpo-33540.wy9LRV.rst
new file mode 100644
index 000000000000..9019cc14f515
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-05-16-18-10-38.bpo-33540.wy9LRV.rst
@@ -0,0 +1,2 @@
+Add a new ``block_on_close`` class attribute to ``ForkingMixIn`` and
+``ThreadingMixIn`` classes of :mod:`socketserver`.



More information about the Python-checkins mailing list