@@ -128,7 +128,6 @@ class will essentially render the service "deaf" while one request is
128
128
import os
129
129
import sys
130
130
import threading
131
- import contextlib
132
131
from io import BufferedIOBase
133
132
from time import monotonic as time
134
133
@@ -629,55 +628,6 @@ def server_close(self):
629
628
self .collect_children (blocking = self .block_on_close )
630
629
631
630
632
- class _Threads (list ):
633
- """
634
- Joinable list of all non-daemon threads.
635
- """
636
- def __init__ (self ):
637
- self ._lock = threading .Lock ()
638
-
639
- def append (self , thread ):
640
- if thread .daemon :
641
- return
642
- with self ._lock :
643
- super ().append (thread )
644
-
645
- def remove (self , thread ):
646
- with self ._lock :
647
- # should not happen, but safe to ignore
648
- with contextlib .suppress (ValueError ):
649
- super ().remove (thread )
650
-
651
- def remove_current (self ):
652
- """Remove a current non-daemon thread."""
653
- thread = threading .current_thread ()
654
- if not thread .daemon :
655
- self .remove (thread )
656
-
657
- def pop_all (self ):
658
- with self ._lock :
659
- self [:], result = [], self [:]
660
- return result
661
-
662
- def join (self ):
663
- for thread in self .pop_all ():
664
- thread .join ()
665
-
666
-
667
- class _NoThreads :
668
- """
669
- Degenerate version of _Threads.
670
- """
671
- def append (self , thread ):
672
- pass
673
-
674
- def join (self ):
675
- pass
676
-
677
- def remove_current (self ):
678
- pass
679
-
680
-
681
631
class ThreadingMixIn :
682
632
"""Mix-in class to handle each request in a new thread."""
683
633
@@ -686,9 +636,9 @@ class ThreadingMixIn:
686
636
daemon_threads = False
687
637
# If true, server_close() waits until all non-daemonic threads terminate.
688
638
block_on_close = True
689
- # Threads object
639
+ # For non-daemonic threads, list of threading.Threading objects
690
640
# used by server_close() to wait for all threads completion.
691
- _threads = _NoThreads ()
641
+ _threads = None
692
642
693
643
def process_request_thread (self , request , client_address ):
694
644
"""Same as in BaseServer but as a thread.
@@ -701,24 +651,27 @@ def process_request_thread(self, request, client_address):
701
651
except Exception :
702
652
self .handle_error (request , client_address )
703
653
finally :
704
- try :
705
- self .shutdown_request (request )
706
- finally :
707
- self ._threads .remove_current ()
654
+ self .shutdown_request (request )
708
655
709
656
def process_request (self , request , client_address ):
710
657
"""Start a new thread to process the request."""
711
- if self .block_on_close :
712
- vars (self ).setdefault ('_threads' , _Threads ())
713
658
t = threading .Thread (target = self .process_request_thread ,
714
659
args = (request , client_address ))
715
660
t .daemon = self .daemon_threads
716
- self ._threads .append (t )
661
+ if not t .daemon and self .block_on_close :
662
+ if self ._threads is None :
663
+ self ._threads = []
664
+ self ._threads .append (t )
717
665
t .start ()
718
666
719
667
def server_close (self ):
720
668
super ().server_close ()
721
- self ._threads .join ()
669
+ if self .block_on_close :
670
+ threads = self ._threads
671
+ self ._threads = None
672
+ if threads :
673
+ for thread in threads :
674
+ thread .join ()
722
675
723
676
724
677
if hasattr (os , "fork" ):
0 commit comments