summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2002-11-22 08:08:44 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2002-11-22 08:08:44 (GMT)
commitf86e8ef33ec22475a051b783b19ee22c5bd3de72 (patch)
tree16617904c9e72eee7e7eea683f7055815c0fe9e8
parente0273de4329d0f442c696891b856d91721816d72 (diff)
downloadcpython-f86e8ef33ec22475a051b783b19ee22c5bd3de72.zip
cpython-f86e8ef33ec22475a051b783b19ee22c5bd3de72.tar.gz
cpython-f86e8ef33ec22475a051b783b19ee22c5bd3de72.tar.bz2
Patch #550765: Add daemon_threads flag.
-rw-r--r--Doc/lib/libsocksvr.tex8
-rw-r--r--Lib/SocketServer.py9
-rw-r--r--Misc/NEWS6
3 files changed, 22 insertions, 1 deletions
diff --git a/Doc/lib/libsocksvr.tex b/Doc/lib/libsocksvr.tex
index 1e51cd6..c51b664 100644
--- a/Doc/lib/libsocksvr.tex
+++ b/Doc/lib/libsocksvr.tex
@@ -37,6 +37,14 @@ handler class. Finally, call the \method{handle_request()} or
\method{serve_forever()} method of the server object to process one or
many requests.
+When inheriting from \class{ThreadingMixIn} for threaded connection
+behavior, you should explicitly declare how you want your threads
+to behave on an abrupt shutdown. The \class{ThreadingMixIn} class
+defines an attribute \var{daemon_threads}, which indicates whether
+or not the server should wait for thread termination. You should
+set the flag explicitly if you would like threads to behave
+autonomously.
+
Server classes have the same external methods and attributes, no
matter what network protocol they use:
diff --git a/Lib/SocketServer.py b/Lib/SocketServer.py
index e1d5ecb..1928ad2 100644
--- a/Lib/SocketServer.py
+++ b/Lib/SocketServer.py
@@ -56,7 +56,8 @@ instance, a threading UDP server class is created as follows:
class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass
The Mix-in class must come first, since it overrides a method defined
-in UDPServer!
+in UDPServer! Setting the various member variables also changes
+the behavior of the underlying server mechanism.
To implement a service, you must derive a class from
BaseRequestHandler and redefine its handle() method. You can then run
@@ -448,6 +449,10 @@ class ForkingMixIn:
class ThreadingMixIn:
"""Mix-in class to handle each request in a new thread."""
+ # Decides how threads will act upon termination of the
+ # main process
+ daemon_threads = 0
+
def process_request_thread(self, request, client_address):
"""Same as in BaseServer but as a thread.
@@ -466,6 +471,8 @@ class ThreadingMixIn:
import threading
t = threading.Thread(target = self.process_request_thread,
args = (request, client_address))
+ if self.daemon_threads:
+ t.setDaemon (1)
t.start()
diff --git a/Misc/NEWS b/Misc/NEWS
index 9fcd57e..42cd9183 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -389,6 +389,12 @@ Extension modules
Library
-------
+- When cancelling a server that implemented threading with a keyboard
+ interrupt, the server would shut down but not terminate (waiting on
+ client threads). A new member variable, daemon_threads, was added to
+ the ThreadingMixIn class in SocketServer.py to make it explicit that
+ this behavior needs to be controlled.
+
- A new module, optparse, provides a fancy alternative to getopt for
command line parsing. It is a slightly modified version of Greg
Ward's Optik package.