diff options
author | Jason R. Coombs <jaraco@jaraco.com> | 2020-12-31 15:19:30 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-31 20:19:30 +0000 |
commit | b5711c940f70af89f2b4cf081a3fcd83924f3ae7 (patch) | |
tree | 18b74a6cbd3091b024db21c842a7162c8fad9578 /Lib/socketserver.py | |
parent | 3631d6deab064de0bb286ef2943885dca3c3075e (diff) | |
download | cpython-b5711c940f70af89f2b4cf081a3fcd83924f3ae7.tar.gz cpython-b5711c940f70af89f2b4cf081a3fcd83924f3ae7.zip |
bpo-37193: Remove thread objects which finished process its request (GH-23127)
This reverts commit aca67da4fe68d5420401ac1782203d302875eb27.
Diffstat (limited to 'Lib/socketserver.py')
-rw-r--r-- | Lib/socketserver.py | 51 |
1 files changed, 39 insertions, 12 deletions
diff --git a/Lib/socketserver.py b/Lib/socketserver.py index 57c1ae6e9e8..0d9583d56a4 100644 --- a/Lib/socketserver.py +++ b/Lib/socketserver.py @@ -628,6 +628,39 @@ if hasattr(os, "fork"): self.collect_children(blocking=self.block_on_close) +class _Threads(list): + """ + Joinable list of all non-daemon threads. + """ + def append(self, thread): + self.reap() + if thread.daemon: + return + super().append(thread) + + def pop_all(self): + self[:], result = [], self[:] + return result + + def join(self): + for thread in self.pop_all(): + thread.join() + + def reap(self): + self[:] = (thread for thread in self if thread.is_alive()) + + +class _NoThreads: + """ + Degenerate version of _Threads. + """ + def append(self, thread): + pass + + def join(self): + pass + + class ThreadingMixIn: """Mix-in class to handle each request in a new thread.""" @@ -636,9 +669,9 @@ class ThreadingMixIn: 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 + # Threads object # used by server_close() to wait for all threads completion. - _threads = None + _threads = _NoThreads() def process_request_thread(self, request, client_address): """Same as in BaseServer but as a thread. @@ -655,23 +688,17 @@ class ThreadingMixIn: def process_request(self, request, client_address): """Start a new thread to process the request.""" + if self.block_on_close: + vars(self).setdefault('_threads', _Threads()) t = threading.Thread(target = self.process_request_thread, args = (request, client_address)) t.daemon = self.daemon_threads - if not t.daemon and self.block_on_close: - if self._threads is None: - self._threads = [] - self._threads.append(t) + self._threads.append(t) t.start() def server_close(self): super().server_close() - if self.block_on_close: - threads = self._threads - self._threads = None - if threads: - for thread in threads: - thread.join() + self._threads.join() if hasattr(os, "fork"): |