Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 1251

Show
Ignore:
Timestamp:
08/18/06 17:23:09
Author:
fumanchu
Message:

Moved some Apache control code from benchmark to _cpmodpy, where it can be more useful.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/_cpmodpy.py

    r1243 r1251  
    11"""Native adapter for serving CherryPy via mod_python""" 
    22 
    3 from mod_python import apache 
    43import cherrypy 
    54from cherrypy._cperror import format_exc, bare_error 
    65from cherrypy.lib import http 
     6 
     7 
     8 
     9# ------------------------------ Request-handling 
    710 
    811 
     
    2629        cherrypy.engine.stop() 
    2730    try: 
     31        from mod_python import apache 
    2832        # apache.register_cleanup wasn't available until 3.1.4. 
    2933        apache.register_cleanup(cherrypy_cleanup) 
     
    4145_isSetUp = False 
    4246def handler(req): 
     47    from mod_python import apache 
    4348    try: 
    4449        global _isSetUp 
     
    120125            req.write(seg) 
    121126 
     127 
     128 
     129# --------------- Startup tools for CherryPy + mod_python --------------- # 
     130 
     131 
     132import os 
     133import re 
     134 
     135 
     136def 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 
     149class ModPythonServer(object): 
     150     
     151    template = """ 
     152# Apache2 server configuration file for running CherryPy with mod_python. 
     153 
     154DocumentRoot "/" 
     155Listen %(port)s 
     156LoadModule 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  
    3232 
    3333import cherrypy 
     34from cherrypy import _cpmodpy 
    3435from cherrypy.lib import http 
    3536 
    3637 
    3738AB_PATH = "" 
    38 APACHE_PATH = "
     39APACHE_PATH = "apache
    3940SCRIPT_NAME = "/cpbench/users/rdelon/apps/blog" 
    4041 
    41 __all__ = ['ABSession', 'Root', 'print_report', 'read_process', 
     42__all__ = ['ABSession', 'Root', 'print_report', 
    4243           'run_standard_benchmarks', 'safe_threads', 
    4344           'size_report', 'startup', 'thread_report', 
     
    101102class NullResponse: 
    102103    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 output 
    116104 
    117105 
     
    202190    def run(self): 
    203191        # 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()) 
    205193        for attr, name, pattern in self.parse_patterns: 
    206194            val = re.search(pattern, self.output, re.MULTILINE) 
     
    273261    """Start the CherryPy app server in 'serverless' mode (for modpython/WSGI).""" 
    274262    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 
    282271        cherrypy.engine.start(blocking=False) 
    283272    if cherrypy.engine.state == cherrypy._cpengine.STARTING: 
     
    285274    return 0 # apache.OK 
    286275 
    287 mp_conf_template = """ 
    288 # Apache2 server configuration file for benchmarking CherryPy with mod_python. 
    289  
    290 DocumentRoot "/" 
    291 Listen 8080 
    292 LoadModule python_module modules/mod_python.so 
    293  
    294 <Location /> 
    295     SetHandler python-program 
    296     PythonFixupHandler cherrypy.test.benchmark::startup_modpython 
    297     PythonHandler modpython_gateway::handler 
    298     PythonOption wsgi.application cherrypy::tree 
    299     PythonDebug On 
    300 %s%s 
    301 </Location> 
    302 """ 
    303  
    304 cpmodpy_template = """ 
    305 # Apache2 server configuration file for benchmarking CherryPy with mod_python. 
    306  
    307 DocumentRoot "/" 
    308 Listen 8080 
    309 LoadModule python_module modules/mod_python.so 
    310  
    311 <Location /> 
    312     SetHandler python-program 
    313     PythonHandler cherrypy._cpmodpy::handler 
    314     PythonOption cherrypy.setup cherrypy.test.benchmark::startup_modpython 
    315     PythonDebug On 
    316 %s%s 
    317 </Location> 
    318 """ 
    319276 
    320277def run_modpython(use_wsgi=False): 
     278    print "Starting mod_python..." 
     279    pyopts = [] 
     280     
    321281    # Pass the null and ab=path options through Apache 
    322     nullreq_opt = "" 
    323282    if "--null" in opts: 
    324         nullreq_opt = "    PythonOption nullreq\n" 
    325      
    326     ab_opt = "" 
     283        pyopts.append(("nullreq", "")) 
     284     
    327285    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 
    330289    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) 
    332294    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     
    337298    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() 
    345300        run() 
    346301    finally: 
    347         os.popen("apache -k stop"
     302        s.stop(
    348303 
    349304 

Hosted by WebFaction

Log in as guest/cpguest to create tickets