forked from tabo/cherrypy-dynpool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cherrypy_dynpool.py
51 lines (39 loc) · 1.46 KB
/
cherrypy_dynpool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import cherrypy
from cherrypy.process.plugins import Monitor
from dynpool import DynamicPoolResizer
try:
from cherrypy.wsgiserver import ThreadPool
except ImportError:
from cheroot.workers.threadpool import ThreadPool
def get_pool_resizer(self, minspare=1, maxspare=5, shrinkfreq=10,
logger=None):
return DynamicPoolResizer(self, minspare, maxspare, shrinkfreq, logger)
ThreadPool.size = property(lambda self: len(self._threads))
ThreadPool.get_pool_resizer = get_pool_resizer
class ThreadPoolMonitor(Monitor):
MINSPARE = 5
MAXSPARE = 15
SHRINKFREQ = 5
def __init__(self, bus):
self._run = lambda: None
super(ThreadPoolMonitor, self).__init__(bus, self.run, frequency=1)
def run(self):
self._run()
def configure(self, thread_pool, logger=None):
minspare = cherrypy.config.get(
'server.thread_pool_minspare', self.MINSPARE)
maxspare = cherrypy.config.get(
'server.thread_pool_maxspare', self.MAXSPARE)
shrinkfreq = cherrypy.config.get(
'server.thread_pool_shrink_frequency', self.SHRINKFREQ)
resizer = thread_pool.get_pool_resizer(
minspare=minspare,
maxspare=maxspare,
shrinkfreq=shrinkfreq,
logger=logger or (lambda: None)
)
self._run = resizer.run
def stop(self):
self._run = lambda: None
super(ThreadPoolMonitor, self).stop()
stop.priority = 10