From 30e8af066f3a091cf58443b8e45068c55bf0d68b Mon Sep 17 00:00:00 2001 From: Helmut Grohne Date: Sun, 10 Mar 2013 14:20:25 +0100 Subject: forkpool: add a per-request timelimit --- wsgitools/scgi/forkpool.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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 -- cgit v1.2.3