Changeset 1431

Show
Ignore:
Timestamp:
02/19/08 12:47:41 (9 months ago)
Author:
JensDiemer
Message:

FastCGI: split the dispatcher file

Location:
trunk/pylucid
Files:
1 added
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/pylucid/index.fcgi

    r1415 r1431  
    1313 
    1414    Note: 
    15     -If this file does not lie in the project folder, you must set PROJECT_DIR! 
     15    -If this file does not lie in the project folder, you must set project_dir. 
    1616    -You need the python package "flup": http://trac.saddi.com/flup 
    1717 
     
    2323        - "Premature end of script headers" 
    2424    try this: 
    25         - Set a LOGFILE 
     25        - Set a logfile 
    2626        - use the runfastcgi() options: 
    2727            daemonize="false" 
     
    3030        - Try to turn on the flup traceback, see: 
    3131            http://code.djangoproject.com/ticket/6610 
     32 
     33    Give feedback in our forum! 
    3234 
    3335    Last commit info: 
     
    4042    :license: GNU GPL v3, see LICENSE.txt for more details. 
    4143""" 
    42 import sys, os, time 
     44from PyLucid.system.fastcgi_server import fastcgi_server 
    4345 
    44 start_overall = time.time() 
     46fastcgi_server( 
     47    # Change into a other project directory? 
     48    # (default: not set -> Don't change the current workdir) 
     49    # e.g.: 
     50    # project_dir="/var/www/htdocs/pylucid/", 
    4551 
    46 # Logging 
    47 # If you enable logging, you should set maxrequests=1 in the runfastcgi method 
    48 # above. Then you see a "alive" log entry (sysexit) afert every request. 
    49 LOGFILE = None # No logging! 
    50 #LOGFILE = "PyLucid_fcgi.log" # Log into this file 
     52    # Logging 
     53    # If you enable logging, you should set "maxrequests"=1 and 
     54    # "daemonize":"false" in runfastcgi_kwargs. Then you see a "alive" log 
     55    # entry (sysexit) after every request. 
     56    # (default: not set -> No logging) 
     57    # e.g.: 
     58    #logfile="PyLucid_fcgi.log", 
    5159 
    52 # Change into a other directory? 
    53 PROJECT_DIR = None # No chdir needed 
    54 #PROJECT_DIR = "/var/www/htdocs/pylucid/" # Change into this directory 
     60    # The DJANGO_SETTINGS_MODULE environment variable. 
     61    # (default: "PyLucid.settings") 
     62    settings_module="PyLucid.settings", 
    5563 
     64    # Keyword arguments for django.core.servers.fastcgi.runfastcgi() 
     65    # (default: empty dict, no options set) 
     66    runfastcgi_kwargs={ 
     67        # prefork or threaded (default "prefork") 
     68        #"method": "prefork", 
    5669 
    57 # Set the DJANGO_SETTINGS_MODULE environment variable. 
    58 os.environ['DJANGO_SETTINGS_MODULE'] = "PyLucid.settings" 
     70        # "true" or "false" whether to detach from terminal 
     71        # Importand: It's not a bool, it's a string! 
     72        #"daemonize": "false", 
    5973 
    60 # Global variable for the last traceback: 
    61 last_tb_info = None 
     74        # Number of requests a child handles before it is killed and a new 
     75        # child is forked. 
     76        # (default: 0 -> no limit) 
     77        # Set to 1 for debugging 
     78        #"maxrequests":1, 
    6279 
    63 #______________________________________________________________________________ 
    64 # Setup logging: 
     80        # max number of spare processes/threads 
     81        #"maxspare":2, 
    6582 
    66 if LOGFILE: 
    67     import logging 
    68     logging.basicConfig( 
    69         level=logging.DEBUG, 
    70         format='%(asctime)s %(levelname)s %(message)s', 
    71         filename=LOGFILE, 
    72         filemode='a' 
    73     ) 
    74     log = logging.debug 
    75     log("--- Logging started ---") 
    76 else: 
    77     # No logging 
    78     def log(*txt): 
    79         pass 
     83        # hard limit number of processes/threads 
     84        #"maxchildren":2, 
    8085 
    81 #______________________________________________________________________________ 
    82 # redirect stderr if logging is on: 
    83  
    84 if LOGFILE: 
    85     try: 
    86         class StdErrorHandler(object): 
    87             """ 
    88             redirects messages from stderr to stdout. 
    89             Sends a header, if the header were not already sent. 
    90             """ 
    91             def __init__(self, out_method): 
    92                 self.out_method = out_method 
    93  
    94             def write(self, *txt): 
    95                 text = "".join([i for i in txt]) 
    96                 for line in text.splitlines(): 
    97                     self.out_method(line) 
    98  
    99         sys.stderr = StdErrorHandler(log) 
    100         sys.stderr.write("stderr redirected into the logfile") 
    101     except Exception, e: 
    102         log("StdErrorHandler error: %s" % e) 
    103  
    104 #______________________________________________________________________________ 
    105  
    106  
    107 def tb_catch_app(environ, start_response): 
    108     """ Minimalistic WSGI app for debugging """ 
    109     start_response('200 OK', [('Content-Type', 'text/html')]) 
    110     yield '<h1>FastCGI Traceback catch</h1>' 
    111  
    112     # Display the overall running time 
    113     yield 'overall time: %.2fsec' % (time.time() - start_overall) 
    114  
    115     # Display the last traceback 
    116     yield '<h2>last_tb_info:</h2>' 
    117     try: 
    118         yield "<pre>%s</pre>" % last_tb_info 
    119     except Exception, e: 
    120         yield "Traceback error: %s" % e 
    121  
    122     yield '<hr />' 
    123  
    124     from cgi import escape 
    125     yield '<h1>FastCGI Environment</h1><table>' 
    126     for k, v in sorted(environ.items()): 
    127         yield '<tr><th>%s</th><td>%s</td></tr>' % ( 
    128             escape(repr(k)), escape(repr(v)) 
    129         ) 
    130     yield '</table>' 
    131  
    132 #______________________________________________________________________________ 
    133 # If this file does not lie in the project folder, then you must define the 
    134 # path to the project directory here: 
    135  
    136 if PROJECT_DIR: 
    137     # Switch to the directory of your project. 
    138     log("chdir '%s'" % PROJECT_DIR) 
    139     try: 
    140         os.chdir(PROJECT_DIR) 
    141     except Exception, e: 
    142         log("chdir error:", e) 
    143  
    144     # Add a custom Python path, you'll want to add the parent folder of 
    145     # your project directory. (Optional.) 
    146     try: 
    147         sys.path.insert(0, PROJECT_DIR) 
    148     except Exception, e: 
    149         log("path.insert error:", e) 
    150  
    151 #______________________________________________________________________________ 
    152  
    153 from django.core.handlers.wsgi import WSGIHandler 
    154 from django.core.servers.fastcgi import runfastcgi 
    155  
    156 try: 
    157     log('runfastcgi()') 
    158     runfastcgi( 
    159         #method="prefork",  # prefork or threaded (default prefork) 
    160         #daemonize="false", # Bool, whether to detach from terminal 
    161         #maxrequests=1,     # number of requests a child handles before it is 
    162                             # killed and a new child is forked (0 = no limit) 
    163         #maxspare=2,        # max number of spare processes/threads 
    164         #maxchildren=2      # hard limit number of processes/threads 
    165         #debug=True,        # Not Implemented, see: 
    166                             # http://code.djangoproject.com/ticket/6610 
    167     ) 
    168 except SystemExit, e: 
    169     log("sys.exit(%s) appears." % e) 
    170 except Exception, e: 
    171     log("fastCGI error: %s" % e) 
    172     import sys, traceback 
    173     last_tb_info = traceback.format_exc() 
    174     log(last_tb_info) 
    175     from flup.server.fcgi import WSGIServer 
    176     WSGIServer(tb_catch_app).run() 
    177 else: 
    178     log("fastcgi application ended.") 
     86        # Not Implemented, see: http://code.djangoproject.com/ticket/6610 
     87        #"debug":True, 
     88    } 
     89)