~ubuntu-branches/ubuntu/jaunty/python-django/jaunty

« back to all changes in this revision

Viewing changes to django/core/servers/fastcgi.py

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant, Eddy Mulyono
  • Date: 2008-09-16 12:18:47 UTC
  • mfrom: (1.1.5 upstream) (4.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080916121847-mg225rg5mnsdqzr0
Tags: 1.0-1ubuntu1
* Merge from Debian (LP: #264191), remaining changes:
  - Run test suite on build.

[Eddy Mulyono]
* Update patch to workaround network test case failures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
"""
2
 
FastCGI server that implements the WSGI protocol.
 
2
FastCGI (or SCGI, or AJP1.3 ...) server that implements the WSGI protocol.
3
3
 
4
4
Uses the flup python package: http://www.saddi.com/software/flup/
5
5
 
17
17
__version__ = "0.1"
18
18
__all__ = ["runfastcgi"]
19
19
 
20
 
FASTCGI_HELP = r"""runfcgi:
21
 
  Run this project as a fastcgi application. To do this, the
22
 
  flup package from http://www.saddi.com/software/flup/ is
23
 
  required.
 
20
FASTCGI_HELP = r"""
 
21
  Run this project as a fastcgi (or some other protocol supported
 
22
  by flup) application. To do this, the flup package from
 
23
  http://www.saddi.com/software/flup/ is required.
24
24
 
25
 
Usage:
26
 
   django-admin.py runfcgi --settings=yourproject.settings [fcgi settings]
27
 
   manage.py runfcgi [fcgi settings]
 
25
   runfcgi [options] [fcgi settings]
28
26
 
29
27
Optional Fcgi settings: (setting=value)
 
28
  protocol=PROTOCOL    fcgi, scgi, ajp, ... (default fcgi)
30
29
  host=HOSTNAME        hostname to listen on..
31
30
  port=PORTNUM         port to listen on.
32
31
  socket=FILE          UNIX socket to listen on.
38
37
  maxchildren=NUMBER   hard limit number of processes / threads
39
38
  daemonize=BOOL       whether to detach from terminal.
40
39
  pidfile=FILE         write the spawned process-id to this file.
41
 
  workdir=DIRECTORY    change to this directory when daemonizing
 
40
  workdir=DIRECTORY    change to this directory when daemonizing.
 
41
  outlog=FILE          write stdout to this file.
 
42
  errlog=FILE          write stderr to this file.
 
43
  umask=UMASK          umask to use when daemonizing (default 022).
42
44
 
43
45
Examples:
44
46
  Run a "standard" fastcgi process on a file-descriptor
45
47
  (for webservers which spawn your processes for you)
46
48
    $ manage.py runfcgi method=threaded
47
49
 
48
 
  Run a fastcgi server on a TCP host/port
49
 
    $ manage.py runfcgi method=prefork host=127.0.0.1 port=8025
 
50
  Run a scgi server on a TCP host/port
 
51
    $ manage.py runfcgi protocol=scgi method=prefork host=127.0.0.1 port=8025
50
52
 
51
53
  Run a fastcgi server on a UNIX domain socket (posix platforms only)
52
54
    $ manage.py runfcgi method=prefork socket=/tmp/fcgi.sock
58
60
"""
59
61
 
60
62
FASTCGI_OPTIONS = {
 
63
    'protocol': 'fcgi',
61
64
    'host': None,
62
65
    'port': None,
63
66
    'socket': None,
69
72
    'minspare': 2,
70
73
    'maxchildren': 50,
71
74
    'maxrequests': 0,
 
75
    'outlog': None,
 
76
    'errlog': None,
 
77
    'umask': None,
72
78
}
73
79
 
74
80
def fastcgi_help(message=None):
100
106
        print >> sys.stderr, "  installed flup, then make sure you have it in your PYTHONPATH."
101
107
        return False
102
108
 
 
109
    flup_module = 'server.' + options['protocol']
 
110
 
103
111
    if options['method'] in ('prefork', 'fork'):
104
 
        from flup.server.fcgi_fork import WSGIServer
105
112
        wsgi_opts = {
106
113
            'maxSpare': int(options["maxspare"]),
107
114
            'minSpare': int(options["minspare"]),
108
115
            'maxChildren': int(options["maxchildren"]),
109
116
            'maxRequests': int(options["maxrequests"]), 
110
117
        }
 
118
        flup_module += '_fork'
111
119
    elif options['method'] in ('thread', 'threaded'):
112
 
        from flup.server.fcgi import WSGIServer
113
120
        wsgi_opts = {
114
121
            'maxSpare': int(options["maxspare"]),
115
122
            'minSpare': int(options["minspare"]),
120
127
 
121
128
    wsgi_opts['debug'] = False # Turn off flup tracebacks
122
129
 
 
130
    try:
 
131
        WSGIServer = getattr(__import__('flup.' + flup_module, '', '', flup_module), 'WSGIServer')
 
132
    except:
 
133
        print "Can't import flup." + flup_module
 
134
        return False
 
135
 
123
136
    # Prep up and go
124
137
    from django.core.handlers.wsgi import WSGIHandler
125
138
 
143
156
        else:
144
157
            return fastcgi_help("ERROR: Invalid option for daemonize parameter.")
145
158
 
 
159
    daemon_kwargs = {}
 
160
    if options['outlog']:
 
161
        daemon_kwargs['out_log'] = options['outlog']
 
162
    if options['errlog']:
 
163
        daemon_kwargs['err_log'] = options['errlog']
 
164
    if options['umask']:
 
165
        daemon_kwargs['umask'] = int(options['umask'])
 
166
 
146
167
    if daemonize:
147
168
        from django.utils.daemonize import become_daemon
148
 
        become_daemon(our_home_dir=options["workdir"])
 
169
        become_daemon(our_home_dir=options["workdir"], **daemon_kwargs)
149
170
 
150
171
    if options["pidfile"]:
151
172
        fp = open(options["pidfile"], "w")