comparison MoinMoin/server/server_standalone.py @ 4063:d996f53790bb

standalone server: use daemon threads so Ctrl-C/kill works better, remove leftover debug code (thanks to Greg Ward for the patch)
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 31 Aug 2008 23:11:02 +0200
parents a48929a5036c
children b3602f5e2038
comparison
equal deleted inserted replaced
4062:e5778919c0a2 4063:d996f53790bb
131 self.lock.wait() 131 self.lock.wait()
132 if self._abort: 132 if self._abort:
133 return 133 return
134 t = Thread(target=self.process_request_thread, 134 t = Thread(target=self.process_request_thread,
135 args=(request, client_address)) 135 args=(request, client_address))
136 t.setDaemon(True)
136 t.start() 137 t.start()
137 finally: 138 finally:
138 self.lock.release() 139 self.lock.release()
139 140
140 def process_request_thread(self, request, client_address): 141 def process_request_thread(self, request, client_address):
162 of creating new thread for each request. This is similar to Apache 163 of creating new thread for each request. This is similar to Apache
163 worker mpm, with a simpler constant thread pool. 164 worker mpm, with a simpler constant thread pool.
164 165
165 This server is 5 times faster than ThreadingServer for static 166 This server is 5 times faster than ThreadingServer for static
166 files, and about the same for wiki pages. 167 files, and about the same for wiki pages.
167
168 TODO: sometimes the server won't exit on Conrol-C, and continue to
169 run with few threads (you can kill it with kill -9). Same problem
170 exist with the twisted server. When the problem is finally solved,
171 remove the commented debug prints.
172 """ 168 """
173 use_threads = True 169 use_threads = True
174 170
175 def __init__(self, config): 171 def __init__(self, config):
176 self.queue = [] 172 self.queue = []
184 def serve_forever(self): 180 def serve_forever(self):
185 """ Create a thread pool then invoke base class method """ 181 """ Create a thread pool then invoke base class method """
186 from threading import Thread 182 from threading import Thread
187 for dummy in range(self.poolSize): 183 for dummy in range(self.poolSize):
188 t = Thread(target=self.serve_forever_thread) 184 t = Thread(target=self.serve_forever_thread)
185 t.setDaemon(True)
189 t.start() 186 t.start()
190 SimpleServer.serve_forever(self) 187 SimpleServer.serve_forever(self)
191 188
192 def process_request(self, request, client_address): 189 def process_request(self, request, client_address):
193 """ Called for each request 190 """ Called for each request
217 try: 214 try:
218 self.finish_request(request, client_address) 215 self.finish_request(request, client_address)
219 except: 216 except:
220 self.handle_error(request, client_address) 217 self.handle_error(request, client_address)
221 self.close_request(request) 218 self.close_request(request)
222 # sys.stderr.write('thread exiting...\n')
223 219
224 def pop_request(self): 220 def pop_request(self):
225 """ Pop a request from the queue 221 """ Pop a request from the queue
226 222
227 If the queue is empty, wait for notification. If the queue was 223 If the queue is empty, wait for notification. If the queue was
238 return item 234 return item
239 except IndexError: 235 except IndexError:
240 self.lock.wait() 236 self.lock.wait()
241 finally: 237 finally:
242 self.lock.release() 238 self.lock.release()
243 # sys.stderr.write('thread exiting...\n')
244 sys.exit() 239 sys.exit()
245 240
246 def die(self): 241 def die(self):
247 """ Wake all threads then invoke base class die 242 """ Wake all threads then invoke base class die
248 243
254 SimpleServer.die(self) 249 SimpleServer.die(self)
255 250
256 def wake_all_threads(self): 251 def wake_all_threads(self):
257 self.lock.acquire() 252 self.lock.acquire()
258 try: 253 try:
259 # sys.stderr.write('waking up all threads...\n')
260 self.lock.notifyAll() 254 self.lock.notifyAll()
261 finally: 255 finally:
262 self.lock.release() 256 self.lock.release()
263 257
264 258