Dolda2000 GitWeb
/
ashd.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
8db4188
)
python: Added an optional request limiter for freethread.
author
Fredrik Tolf
<fredrik@dolda2000.com>
Sun, 5 Jan 2014 10:45:14 +0000
(11:45 +0100)
committer
Fredrik Tolf
<fredrik@dolda2000.com>
Sun, 5 Jan 2014 10:45:14 +0000
(11:45 +0100)
python3/ashd/serve.py
patch
|
blob
|
blame
|
history
diff --git
a/python3/ashd/serve.py
b/python3/ashd/serve.py
index
8520411
..
6111ec6
100644
(file)
--- a/
python3/ashd/serve.py
+++ b/
python3/ashd/serve.py
@@
-86,13
+86,28
@@
class handler(object):
return {}
class freethread(handler):
return {}
class freethread(handler):
- def __init__(self, **kw):
+ def __init__(self, *
, max=None, *
*kw):
super().__init__(**kw)
self.current = set()
self.lk = threading.Lock()
super().__init__(**kw)
self.current = set()
self.lk = threading.Lock()
+ self.tcond = threading.Condition(self.lk)
+ self.max = max
+
+ @classmethod
+ def parseargs(cls, *, max=None, **args):
+ ret = super().parseargs(**args)
+ if max:
+ ret["max"] = int(max)
+ return ret
def handle(self, req):
def handle(self, req):
- reqthread(target=self.run, args=[req]).start()
+ with self.lk:
+ while self.max is not None and len(self.current) >= self.max:
+ self.tcond.wait()
+ th = reqthread(target=self.run, args=[req])
+ th.start()
+ while th.is_alive() and th not in self.current:
+ self.tcond.wait()
def ckflush(self, req):
while len(req.buffer) > 0:
def ckflush(self, req):
while len(req.buffer) > 0:
@@
-104,6
+119,7
@@
class freethread(handler):
th = threading.current_thread()
with self.lk:
self.current.add(th)
th = threading.current_thread()
with self.lk:
self.current.add(th)
+ self.tcond.notify_all()
try:
env = req.mkenv()
with perf.request(env) as reqevent:
try:
env = req.mkenv()
with perf.request(env) as reqevent:
@@
-121,6
+137,7
@@
class freethread(handler):
finally:
with self.lk:
self.current.remove(th)
finally:
with self.lk:
self.current.remove(th)
+ self.tcond.notify_all()
finally:
req.close()
finally:
req.close()