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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
"""Create and manage the CherryPy application engine."""
import cgi
import os
import signal
import sys
import threading
import time
import cherrypy
from cherrypy import _cprequest
# Use a flag to indicate the state of the application engine.
STOPPED = 0
STARTING = None
STARTED = 1
def fileattr(m):
if hasattr(m, "__loader__"):
if hasattr(m.__loader__, "archive"):
return m.__loader__.archive
return getattr(m, "__file__", None)
def signal_handler(signum, frame):
if signum in (signal.SIGHUP,):
cherrypy.engine.reexec()
else:
cherrypy.server.stop()
cherrypy.engine.stop()
signal.signal(signal.SIGHUP, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
class Engine(object):
"""The application engine, which exposes a request interface to (HTTP) servers."""
request_class = _cprequest.Request
response_class = _cprequest.Response
def __init__(self):
self.state = STOPPED
# Startup/shutdown hooks
self.on_start_engine_list = []
self.on_stop_engine_list = []
self.on_start_thread_list = []
self.on_stop_thread_list = []
self.seen_threads = {}
self.mtimes = {}
self.reload_files = []
def start(self, blocking=True):
"""Start the application engine."""
self.state = STARTING
conf = cherrypy.config.get
# Output config options to log
if conf("log_config", True):
cherrypy.config.log_config()
for func in self.on_start_engine_list:
func()
self.state = STARTED
if blocking:
self.block()
def block(self):
"""Block forever (wait for stop(), KeyboardInterrupt or SystemExit)."""
try:
autoreload = cherrypy.config.get('autoreload.on', False)
if autoreload:
i = 0
freq = cherrypy.config.get('autoreload.frequency', 1)
while self.state != STOPPED:
time.sleep(.1)
# Autoreload
if autoreload:
i += .1
if i > freq:
i = 0
self.autoreload()
except KeyboardInterrupt:
cherrypy.log("<Ctrl-C> hit: shutting down app engine", "ENGINE")
cherrypy.server.stop()
self.stop()
except SystemExit:
cherrypy.log("SystemExit raised: shutting down app engine", "ENGINE")
cherrypy.server.stop()
self.stop()
raise
except:
# Don't bother logging, since we're going to re-raise.
# Note that we don't stop the HTTP server here.
self.stop()
raise
def reexec(self):
"""Re-execute the current process."""
cherrypy.server.stop()
self.stop()
args = sys.argv[:]
cherrypy.log("Re-spawning %s" % " ".join(args), "ENGINE")
args.insert(0, sys.executable)
if sys.platform == "win32":
args = ['"%s"' % arg for arg in args]
os.execv(sys.executable, args)
def autoreload(self):
"""Reload the process if registered files have been modified."""
for filename in map(fileattr, sys.modules.values()) + self.reload_files:
if filename:
if filename.endswith(".pyc"):
filename = filename[:-1]
try:
mtime = os.stat(filename).st_mtime
except OSError:
if filename in self.mtimes:
# The file was probably deleted.
self.reexec()
if filename not in self.mtimes:
self.mtimes[filename] = mtime
continue
if mtime > self.mtimes[filename]:
# The file has been modified.
self.reexec()
def stop(self):
"""Stop the application engine."""
if self.state != STOPPED:
for thread_ident, i in self.seen_threads.iteritems():
for func in self.on_stop_thread_list:
func(i)
self.seen_threads.clear()
for func in self.on_stop_engine_list:
func()
self.state = STOPPED
cherrypy.log("CherryPy shut down", "ENGINE")
def restart(self):
"""Restart the application engine (doesn't block)."""
self.stop()
self.start(blocking=False)
def wait(self):
"""Block the caller until ready to receive requests (or error)."""
while not self.ready:
time.sleep(.1)
def _is_ready(self):
return bool(self.state == STARTED)
ready = property(_is_ready, doc="Return True if the engine is ready to"
" receive requests, False otherwise.")
def request(self, client_address, remote_host, scheme="http"):
"""Obtain an HTTP Request object.
client_address: the (IP address, port) of the client
remote_host should be the client's host name. If not available
(because no reverse DNS lookup is performed), the client
IP should be provided.
scheme: either "http" or "https"; defaults to "http"
"""
if self.state == STOPPED:
r = NotReadyRequest("The CherryPy engine has stopped.")
elif self.state == STARTING:
r = NotReadyRequest("The CherryPy engine could not start.")
else:
# Only run on_start_thread_list if the engine is running.
threadID = threading._get_ident()
if threadID not in self.seen_threads:
i = len(self.seen_threads) + 1
self.seen_threads[threadID] = i
for func in self.on_start_thread_list:
func(i)
r = self.request_class(client_address[0], client_address[1],
remote_host, scheme)
cherrypy.serving.request = r
cherrypy.serving.response = self.response_class()
return r
def start_with_callback(self, func, args=None, kwargs=None):
"""Start, then callback the given func in a new thread."""
if args is None:
args = ()
if kwargs is None:
kwargs = {}
args = (func,) + args
def _callback(func, *a, **kw):
self.wait()
func(*a, **kw)
t = threading.Thread(target=_callback, args=args, kwargs=kwargs)
t.setName("CPEngine Callback " + t.getName())
t.start()
self.start()
class NotReadyRequest:
def __init__(self, msg):
self.msg = msg
def close(self):
pass
def run(self, request_line, headers, rfile):
self.method = "GET"
cherrypy.HTTPError(503, self.msg).set_response()
cherrypy.response.finalize()
return cherrypy.response
|