pdm = None
def usage(out):
- out.write("usage: ashd-wsgi3 [-hAL] [-m PDM-SPEC] [-p MODPATH] [-l REQLIMIT] HANDLER-MODULE [ARGS...]\n")
+ out.write("usage: ashd-wsgi3 [-hAL] [-m PDM-SPEC] [-p MODPATH] [-t REQUEST-HANDLER[:PAR[=VAL](,PAR[=VAL])...]] HANDLER-MODULE [ARGS...]\n")
-reqlimit = 0
+hspec = "free", {}
modwsgi_compat = False
setlog = True
-opts, args = getopt.getopt(sys.argv[1:], "+hALp:l:m:")
+opts, args = getopt.getopt(sys.argv[1:], "+hALp:t:m:")
for o, a in opts:
if o == "-h":
usage(sys.stdout)
setlog = False
elif o == "-A":
modwsgi_compat = True
- elif o == "-l":
- reqlimit = int(a)
+ elif o == "-t":
+ hspec = ashd.serve.parsehspec(a)
elif o == "-m":
if pdm is not None:
pdm.srv.listen(a)
env["wsgi.run_once"] = False
return env
-if reqlimit != 0:
- guard = ashd.serve.abortlimiter(reqlimit).call
-else:
- guard = lambda fun: fun()
-
def recode(thing):
if isinstance(thing, collections.ByteString):
return thing
else:
return str(thing).encode("latin-1")
-reqhandler = ashd.serve.freethread()
-
class request(ashd.serve.wsgirequest):
def __init__(self, *, bkreq, **kw):
super().__init__(**kw)
def handle(req):
reqhandler.handle(request(bkreq=req, handler=reqhandler))
+if hspec[0] not in ashd.serve.names:
+ sys.stderr.write("ashd-wsgi3: no such request handler: %s\n" % hspec[0])
+ sys.exit(1)
+hclass = ashd.serve.names[hspec[0]]
+try:
+ hargs = hclass.parseargs(**hspec[1])
+except ValueError as exc:
+ sys.stderr.write("ashd-wsgi3: %s\n" % exc)
+ sys.exit(1)
+
+reqhandler = hclass(**hargs)
try:
ashd.util.serveloop(handle)
finally:
def close(self):
pass
+ @classmethod
+ def parseargs(cls, **args):
+ if len(args) > 0:
+ raise ValueError("unknown handler argument: " + next(iter(args)))
+ return {}
+
class freethread(handler):
def __init__(self, **kw):
super().__init__(**kw)
if len(self.current) > 0:
th = next(iter(self.current))
else:
- th = None
+ return
th.join()
class threadpool(handler):
- def __init__(self, *, min=0, max=20, live=10, **kw):
+ def __init__(self, *, min=0, max=20, live=300, **kw):
super().__init__(**kw)
self.current = set()
self.free = set()
for i in range(self.min):
self.newthread()
+ @classmethod
+ def parseargs(cls, *, min=None, max=None, live=None, **args):
+ ret = super().parseargs(**args)
+ if min:
+ ret["min"] = int(min)
+ if max:
+ ret["max"] = int(max)
+ if live:
+ ret["live"] = int(live)
+ return ret
+
def newthread(self):
with self.lk:
th = reqthread(target=self.loop)
names = {"free": freethread,
"pool": threadpool}
+
+def parsehspec(spec):
+ if ":" not in spec:
+ return spec, {}
+ nm, spec = spec.split(":", 1)
+ args = {}
+ while spec:
+ if "," in spec:
+ part, spec = spec.split(",", 1)
+ else:
+ part, spec = spec, None
+ if "=" in part:
+ key, val = part.split("=", 1)
+ else:
+ key, val = part, ""
+ args[key] = val
+ return nm, args
pdm = None
def usage(out):
- out.write("usage: scgi-wsgi3 [-hAL] [-m PDM-SPEC] [-p MODPATH] [-T [HOST:]PORT] HANDLER-MODULE [ARGS...]\n")
+ out.write("usage: scgi-wsgi3 [-hAL] [-m PDM-SPEC] [-p MODPATH] [-t REQUEST-HANDLER[:PAR[=VAL](,PAR[=VAL])...]] [-T [HOST:]PORT] HANDLER-MODULE [ARGS...]\n")
sk = None
+hspec = "free", {}
modwsgi_compat = False
setlog = True
-opts, args = getopt.getopt(sys.argv[1:], "+hALp:T:m:")
+opts, args = getopt.getopt(sys.argv[1:], "+hALp:t:T:m:")
for o, a in opts:
if o == "-h":
usage(sys.stdout)
elif o == "-m":
if pdm is not None:
pdm.srv.listen(a)
+ elif o == "-t":
+ hspec = ashd.serve.parsehspec(a)
if len(args) < 1:
usage(sys.stderr)
sys.exit(1)
else:
return str(thing).encode("latin-1")
-reqhandler = ashd.serve.freethread()
-
class request(ashd.serve.wsgirequest):
def __init__(self, *, sk, **kw):
super().__init__(**kw)
self.sk.close()
self.bsk.close()
+if hspec[0] not in ashd.serve.names:
+ sys.stderr.write("ashd-wsgi3: no such request handler: %s\n" % hspec[0])
+ sys.exit(1)
+hclass = ashd.serve.names[hspec[0]]
+try:
+ hargs = hclass.parseargs(**hspec[1])
+except ValueError as exc:
+ sys.stderr.write("ashd-wsgi3: %s\n" % exc)
+ sys.exit(1)
+
+reqhandler = hclass(**hargs)
try:
while True:
nsk, addr = sk.accept()