Changeset 1251
- Timestamp:
- 08/18/06 17:23:09
- Files:
-
- trunk/cherrypy/_cpmodpy.py (modified) (4 diffs)
- trunk/cherrypy/test/benchmark.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/_cpmodpy.py
r1243 r1251 1 1 """Native adapter for serving CherryPy via mod_python""" 2 2 3 from mod_python import apache4 3 import cherrypy 5 4 from cherrypy._cperror import format_exc, bare_error 6 5 from cherrypy.lib import http 6 7 8 9 # ------------------------------ Request-handling 7 10 8 11 … … 26 29 cherrypy.engine.stop() 27 30 try: 31 from mod_python import apache 28 32 # apache.register_cleanup wasn't available until 3.1.4. 29 33 apache.register_cleanup(cherrypy_cleanup) … … 41 45 _isSetUp = False 42 46 def handler(req): 47 from mod_python import apache 43 48 try: 44 49 global _isSetUp … … 120 125 req.write(seg) 121 126 127 128 129 # --------------- Startup tools for CherryPy + mod_python --------------- # 130 131 132 import os 133 import re 134 135 136 def read_process(cmd, args=""): 137 pipein, pipeout = os.popen4("%s %s" % (cmd, args)) 138 try: 139 firstline = pipeout.readline() 140 if (re.search(r"(not recognized|No such file|not found)", firstline, 141 re.IGNORECASE)): 142 raise IOError('%s must be on your system path.' % cmd) 143 output = firstline + pipeout.read() 144 finally: 145 pipeout.close() 146 return output 147 148 149 class ModPythonServer(object): 150 151 template = """ 152 # Apache2 server configuration file for running CherryPy with mod_python. 153 154 DocumentRoot "/" 155 Listen %(port)s 156 LoadModule python_module modules/mod_python.so 157 158 <Location %(loc)s> 159 SetHandler python-program 160 PythonHandler %(handler)s 161 PythonDebug On 162 %(opts)s 163 </Location> 164 """ 165 166 def __init__(self, loc="/", port=80, opts=None, apache_path="apache", 167 handler="cherrypy._cpmodpy::handler"): 168 self.loc = loc 169 self.port = port 170 self.opts = opts 171 self.apache_path = apache_path 172 self.handler = handler 173 174 def start(self): 175 opts = "".join([" PythonOption %s %s\n" % (k, v) 176 for k, v in self.opts]) 177 conf_data = self.template % {"port": self.port, 178 "loc": self.loc, 179 "opts": opts, 180 "handler": self.handler, 181 } 182 183 mpconf = os.path.join(os.path.dirname(__file__), "cpmodpy.conf") 184 f = open(mpconf, 'wb') 185 try: 186 f.write(conf_data) 187 finally: 188 f.close() 189 190 response = read_process(self.apache_path, "-k start -f %s" % mpconf) 191 self.ready = True 192 return response 193 194 def stop(self): 195 os.popen("apache -k stop") 196 self.ready = False 197 trunk/cherrypy/test/benchmark.py
r1249 r1251 32 32 33 33 import cherrypy 34 from cherrypy import _cpmodpy 34 35 from cherrypy.lib import http 35 36 36 37 37 38 AB_PATH = "" 38 APACHE_PATH = " "39 APACHE_PATH = "apache" 39 40 SCRIPT_NAME = "/cpbench/users/rdelon/apps/blog" 40 41 41 __all__ = ['ABSession', 'Root', 'print_report', 'read_process',42 __all__ = ['ABSession', 'Root', 'print_report', 42 43 'run_standard_benchmarks', 'safe_threads', 43 44 'size_report', 'startup', 'thread_report', … … 101 102 class NullResponse: 102 103 pass 103 104 105 def read_process(cmd, args=""):106 pipein, pipeout = os.popen4("%s %s" % (cmd, args))107 try:108 firstline = pipeout.readline()109 if (re.search(r"(not recognized|No such file|not found)", firstline,110 re.IGNORECASE)):111 raise IOError('%s must be on your system path.' % cmd)112 output = firstline + pipeout.read()113 finally:114 pipeout.close()115 return output116 104 117 105 … … 202 190 def run(self): 203 191 # Parse output of ab, setting attributes on self 204 self.output = read_process(AB_PATH or "ab", self.args())192 self.output = _cpmodpy.read_process(AB_PATH or "ab", self.args()) 205 193 for attr, name, pattern in self.parse_patterns: 206 194 val = re.search(pattern, self.output, re.MULTILINE) … … 273 261 """Start the CherryPy app server in 'serverless' mode (for modpython/WSGI).""" 274 262 if cherrypy.engine.state == cherrypy._cpengine.STOPPED: 275 if req.get_options().has_key("nullreq"): 276 cherrypy.engine.request_class = NullRequest 277 cherrypy.engine.response_class = NullResponse 278 ab_opt = req.get_options().get("ab", "") 279 if ab_opt: 280 global AB_PATH 281 AB_PATH = ab_opt 263 if req: 264 if req.get_options().has_key("nullreq"): 265 cherrypy.engine.request_class = NullRequest 266 cherrypy.engine.response_class = NullResponse 267 ab_opt = req.get_options().get("ab", "") 268 if ab_opt: 269 global AB_PATH 270 AB_PATH = ab_opt 282 271 cherrypy.engine.start(blocking=False) 283 272 if cherrypy.engine.state == cherrypy._cpengine.STARTING: … … 285 274 return 0 # apache.OK 286 275 287 mp_conf_template = """288 # Apache2 server configuration file for benchmarking CherryPy with mod_python.289 290 DocumentRoot "/"291 Listen 8080292 LoadModule python_module modules/mod_python.so293 294 <Location />295 SetHandler python-program296 PythonFixupHandler cherrypy.test.benchmark::startup_modpython297 PythonHandler modpython_gateway::handler298 PythonOption wsgi.application cherrypy::tree299 PythonDebug On300 %s%s301 </Location>302 """303 304 cpmodpy_template = """305 # Apache2 server configuration file for benchmarking CherryPy with mod_python.306 307 DocumentRoot "/"308 Listen 8080309 LoadModule python_module modules/mod_python.so310 311 <Location />312 SetHandler python-program313 PythonHandler cherrypy._cpmodpy::handler314 PythonOption cherrypy.setup cherrypy.test.benchmark::startup_modpython315 PythonDebug On316 %s%s317 </Location>318 """319 276 320 277 def run_modpython(use_wsgi=False): 278 print "Starting mod_python..." 279 pyopts = [] 280 321 281 # Pass the null and ab=path options through Apache 322 nullreq_opt = ""323 282 if "--null" in opts: 324 nullreq_opt = " PythonOption nullreq\n" 325 326 ab_opt = "" 283 pyopts.append(("nullreq", "")) 284 327 285 if "--ab" in opts: 328 ab_opt = " PythonOption ab %s\n" % opts["--ab"] 329 286 pyopts.append(("ab", opts["--ab"])) 287 288 s = _cpmodpy.ModPythonServer 330 289 if use_wsgi: 331 conf_data = mp_conf_template % (ab_opt, nullreq_opt) 290 pyopts.append(("wsgi.application", "cherrypy::tree")) 291 pyopts.append(("wsgi.startup", "cherrypy.test.benchmark::startup_modpython")) 292 handler = "modpython_gateway::handler" 293 s = s(port=8080, opts=pyopts, apache_path=APACHE_PATH, handler=handler) 332 294 else: 333 conf_data = cpmodpy_template % (ab_opt, nullreq_opt) 334 mpconf = os.path.join(curdir, "bench_mp.conf") 335 336 f = open(mpconf, 'wb') 295 pyopts.append(("cherrypy.setup", "cherrypy.test.benchmark::startup_modpython")) 296 s = s(port=8080, opts=pyopts, apache_path=APACHE_PATH) 297 337 298 try: 338 f.write(conf_data) 339 finally: 340 f.close() 341 342 apargs = "-k start -f %s" % mpconf 343 try: 344 read_process(APACHE_PATH or "apache", apargs) 299 s.start() 345 300 run() 346 301 finally: 347 os.popen("apache -k stop")302 s.stop() 348 303 349 304

