python: Added a simple, non-threaded handler.
authorFredrik Tolf <fredrik@dolda2000.com>
Sun, 5 Jan 2014 15:04:14 +0000 (16:04 +0100)
committerFredrik Tolf <fredrik@dolda2000.com>
Sun, 5 Jan 2014 15:04:14 +0000 (16:04 +0100)
python3/ashd/serve.py

index 74ccd92..ecc8d46 100644 (file)
@@ -75,7 +75,9 @@ class handler(object):
     def handle(self, request):
         raise Exception()
     def ckflush(self, req):
-        raise Exception()
+        while len(req.buffer) > 0:
+            rls, wls, els = select.select([], [req], [req])
+            req.flush()
     def close(self):
         pass
 
@@ -85,6 +87,25 @@ class handler(object):
             raise ValueError("unknown handler argument: " + next(iter(args)))
         return {}
 
+class single(handler):
+    def handle(self, req):
+        try:
+            env = req.mkenv()
+            with perf.request(env) as reqevent:
+                respiter = req.handlewsgi(env, req.startreq)
+                for data in respiter:
+                    req.write(data)
+                if req.status:
+                    reqevent.response([req.status, req.headers])
+                    req.flushreq()
+                self.ckflush(req)
+        except closed:
+            pass
+        except:
+            log.error("exception occurred when handling request", exc_info=True)
+        finally:
+            req.close()
+
 class freethread(handler):
     def __init__(self, *, max=None, timeout=None, **kw):
         super().__init__(**kw)
@@ -121,11 +142,6 @@ class freethread(handler):
             while th.is_alive() and th not in self.current:
                 self.tcond.wait()
 
-    def ckflush(self, req):
-        while len(req.buffer) > 0:
-            rls, wls, els = select.select([], [req], [req])
-            req.flush()
-
     def run(self, req):
         try:
             th = threading.current_thread()
@@ -195,11 +211,6 @@ class threadpool(handler):
             while not th in self.current:
                 self.pcond.wait()
 
-    def ckflush(self, req):
-        while len(req.buffer) > 0:
-            rls, wls, els = select.select([], [req], [req])
-            req.flush()
-
     def _handle(self, req):
         try:
             env = req.mkenv()
@@ -272,7 +283,8 @@ class threadpool(handler):
                 self.rcond.notify_all()
                 self.pcond.wait(1)
 
-names = {"free": freethread,
+names = {"single": single,
+         "free": freethread,
          "pool": threadpool}
 
 def parsehspec(spec):