summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHelmut Grohne <helmut@subdivi.de>2013-03-10 14:20:25 +0100
committerHelmut Grohne <helmut@subdivi.de>2013-03-10 14:20:25 +0100
commit30e8af066f3a091cf58443b8e45068c55bf0d68b (patch)
treec97d95a43b3fbd8e544d68c726f55ce03d0b9f3a
parentd2f739d40ffe84538e0f743704d2f5c9331a1ba9 (diff)
downloadwsgitools-30e8af066f3a091cf58443b8e45068c55bf0d68b.tar.gz
forkpool: add a per-request timelimit
-rw-r--r--wsgitools/scgi/forkpool.py11
1 files changed, 10 insertions, 1 deletions
diff --git a/wsgitools/scgi/forkpool.py b/wsgitools/scgi/forkpool.py
index f27cd25..514094f 100644
--- a/wsgitools/scgi/forkpool.py
+++ b/wsgitools/scgi/forkpool.py
@@ -182,7 +182,7 @@ class SCGIServer:
def __init__(self, wsgiapp, port, interface="localhost", error=sys.stderr,
minworkers=2, maxworkers=32, maxrequests=1000, config={},
- reusesocket=None, cpulimit=None):
+ reusesocket=None, cpulimit=None, timelimit=None):
"""
@param wsgiapp: is the WSGI application to be run.
@type port: int
@@ -213,6 +213,10 @@ class SCGIServer:
resource limits are available to this platform. After reaching
the soft limit workers will continue to process the current
request and then cleanly terminate.
+ @type timelimit: int
+ @param timelimit: The maximum number of wall clock seconds processing
+ a request should take. If this is specified, an alarm timer is
+ installed and the default action is to kill the worker.
"""
assert hasattr(error, "write")
self.wsgiapp = wsgiapp
@@ -227,6 +231,7 @@ class SCGIServer:
# master: None or a tuple denoting the limit to be configured.
# worker: boolean denoting whether the limit is reached.
self.cpulimit = cpulimit
+ self.timelimit = timelimit
self.server = None # becomes a socket
# maps filedescriptors to WorkerStates
self.workers = {}
@@ -369,7 +374,11 @@ class SCGIServer:
(con, addr) = self.server.accept()
# we cannot handle socket.errors here.
worksock.sendall('1') # tell server we're working
+ if self.timelimit:
+ signal.alarm(self.timelimit)
self.process(con)
+ if self.timelimit:
+ signal.alarm(0)
worksock.sendall('0') # tell server we've finished
if self.cpulimit:
break