diff options
author | Helmut Grohne <helmut@subdivi.de> | 2011-08-19 21:01:52 +0200 |
---|---|---|
committer | Helmut Grohne <helmut@subdivi.de> | 2011-08-19 21:01:52 +0200 |
commit | 6bb3c64529ab99866de5db70f06d00e2c50b30a5 (patch) | |
tree | ea60e6e325ed1c443b9d29f73d3b8e4599a636ea /wsgitools/scgi/forkpool.py | |
parent | 8ab2290c24d90d5ff862b68455d0c7b88d5dedc1 (diff) | |
download | wsgitools-6bb3c64529ab99866de5db70f06d00e2c50b30a5.tar.gz |
scgi: support reusing a listen socket
This is useful when used in combination with e.g. systemd.
Diffstat (limited to 'wsgitools/scgi/forkpool.py')
-rw-r--r-- | wsgitools/scgi/forkpool.py | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/wsgitools/scgi/forkpool.py b/wsgitools/scgi/forkpool.py index b090a54..8ccd579 100644 --- a/wsgitools/scgi/forkpool.py +++ b/wsgitools/scgi/forkpool.py @@ -168,7 +168,8 @@ class SCGIServer: self.state = state def __init__(self, wsgiapp, port, interface="localhost", error=sys.stderr, - minworkers=2, maxworkers=32, maxrequests=1000, config={}): + minworkers=2, maxworkers=32, maxrequests=1000, config={}, + reusesocket=None): """ @param wsgiapp: is the WSGI application to be run. @type port: int @@ -188,6 +189,11 @@ class SCGIServer: @type config: {} @param config: the environ dictionary is updated using these values for each request. + @type reusesocket: None or socket.socket + @param reusesocket: If a socket is passed, do not create a socket. + Instead use given socket as listen socket. The passed socket + must be set up for accepting tcp connections (i.e. AF_INET, + SOCK_STREAM with bind and listen called). """ assert hasattr(error, "write") self.wsgiapp = wsgiapp @@ -198,6 +204,7 @@ class SCGIServer: self.maxrequests = maxrequests self.config = config self.error = error + self.reusesocket = reusesocket self.server = None # becomes a socket # maps filedescriptors to WorkerStates self.workers = {} @@ -218,10 +225,13 @@ class SCGIServer: """ Serve the wsgi application. """ - self.server = socket.socket() - self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - self.server.bind((self.interface, self.port)) - self.server.listen(5) + if self.reusesocket is None: + self.server = socket.socket() + self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + self.server.bind((self.interface, self.port)) + self.server.listen(5) + else: + self.server = self.reusesocket self.running = True while self.running: while (len(self.workers) < self.minworkers or # less than min @@ -254,7 +264,8 @@ class SCGIServer: pid, _ = os.waitpid(0, os.WNOHANG) except OSError: pass - self.server.close() + if self.reusesocket is None: + self.server.close() self.server = None self.killworkers() |