comparison myrss/myrss_app.py @ 70:3456dd3e8660

myrss: add exception handling around main()
author paulo
date Wed, 29 Jul 2015 00:20:39 -0600
parents ae0f2f438a95
children c7bbd3805509
comparison
equal deleted inserted replaced
5:4ab4372f17fc 6:a7ee5c66e71c
4 import urllib2 4 import urllib2
5 import threading 5 import threading
6 import Queue 6 import Queue
7 import datetime 7 import datetime
8 import time 8 import time
9 import traceback
9 10
10 import logging 11 import logging
11 logging.basicConfig( 12 logging.basicConfig(
12 level=logging.INFO, 13 #level=logging.DEBUG,
13 #filename="_LOG", 14 #filename="_LOG",
14 #format="%(asctime)s %(levelname)-8s %(message)s", 15 #format="%(asctime)s %(levelname)-8s %(message)s",
15 ) 16 )
16 17
17 import xml.etree.ElementTree 18 import xml.etree.ElementTree
247 248
248 for _ in range(MAX_THREADS): 249 for _ in range(MAX_THREADS):
249 WorkerThread(input_queue=self._iq, output_queue=self._oq).start() 250 WorkerThread(input_queue=self._iq, output_queue=self._oq).start()
250 251
251 def __call__(self, environ, start_response): 252 def __call__(self, environ, start_response):
252 response_body = main(self._iq, self._oq, self._main_lock) 253 response_code = "500 Internal Server Error"
254 response_type = "text/plain; charset=UTF-8"
255
256 try:
257 response_body = main(self._iq, self._oq, self._main_lock)
258 response_code = "200 OK"
259 response_type = "text/html; charset=UTF-8"
260 except:
261 response_body = traceback.format_exc()
262
253 response_headers = [ 263 response_headers = [
254 ("Content-Type", "text/html; charset=UTF-8"), 264 ("Content-Type", response_type),
255 ("Content-Length", str(len(response_body))), 265 ("Content-Length", str(len(response_body))),
256 ] 266 ]
257 start_response("200 OK", response_headers) 267 start_response(response_code, response_headers)
258 268
259 return [response_body] 269 return [response_body]