Mercurial > hg > index.fcgi > www > www-1
diff myrss/myrss_app.py @ 44:c673e9e9c4ca
add threading.Lock to main() to support threaded server
author | paulo |
---|---|
date | Tue, 05 Feb 2013 00:57:37 -0800 |
parents | df6a1a347584 |
children | aca02ce71274 |
line diff
1.1 --- a/myrss/myrss_app.py Tue Feb 05 00:01:49 2013 -0800 1.2 +++ b/myrss/myrss_app.py Tue Feb 05 00:57:37 2013 -0800 1.3 @@ -16,8 +16,7 @@ 1.4 FEEDS_FILE = "FEEDS" 1.5 CACHE_HTML_FILE = "__cache__.html" 1.6 1.7 -#CACHE_LIFE = 1200 # [seconds] 1.8 -CACHE_LIFE = 30 # [seconds] 1.9 +CACHE_LIFE = 1200 # [seconds] 1.10 MAX_ITEMS = 30 1.11 MAX_LINK_Z = 4 1.12 MAX_THREADS = 20 1.13 @@ -158,36 +157,37 @@ 1.14 self._output_queue.put((idx, docfeed)) 1.15 1.16 1.17 -def main(input_queue, output_queue): 1.18 +def main(input_queue, output_queue, lock): 1.19 ret = '' 1.20 1.21 - epoch_now = time.time() 1.22 - dtnow = datetime.datetime.fromtimestamp(epoch_now) 1.23 + with lock: 1.24 + epoch_now = time.time() 1.25 + dtnow = datetime.datetime.fromtimestamp(epoch_now) 1.26 1.27 - if os.path.exists(CACHE_HTML_FILE) and (epoch_now - os.stat(CACHE_HTML_FILE).st_mtime) < float(CACHE_LIFE): 1.28 - with open(CACHE_HTML_FILE) as cache_html_file: 1.29 - ret = cache_html_file.read() 1.30 + if os.path.exists(CACHE_HTML_FILE) and (epoch_now - os.stat(CACHE_HTML_FILE).st_mtime) < float(CACHE_LIFE): 1.31 + with open(CACHE_HTML_FILE) as cache_html_file: 1.32 + ret = cache_html_file.read() 1.33 1.34 - else: 1.35 - with open(FEEDS_FILE) as feeds_file: 1.36 - feedlines = feeds_file.readlines() 1.37 + else: 1.38 + with open(FEEDS_FILE) as feeds_file: 1.39 + feedlines = feeds_file.readlines() 1.40 1.41 - docstruct = [None]*len(feedlines) 1.42 - num_input = 0 1.43 - for (i, l) in enumerate(feedlines): 1.44 - if l[0] != '#': 1.45 - l = l.strip() 1.46 - input_queue.put((i, l)) 1.47 - num_input += 1 1.48 + docstruct = [None]*len(feedlines) 1.49 + num_input = 0 1.50 + for (i, l) in enumerate(feedlines): 1.51 + if l[0] != '#': 1.52 + l = l.strip() 1.53 + input_queue.put((i, l)) 1.54 + num_input += 1 1.55 1.56 - for _ in range(num_input): 1.57 - (idx, docfeed) = output_queue.get() 1.58 - docstruct[idx] = docfeed 1.59 + for _ in range(num_input): 1.60 + (idx, docfeed) = output_queue.get() 1.61 + docstruct[idx] = docfeed 1.62 1.63 - ret = _to_html(dtnow, docstruct) 1.64 + ret = _to_html(dtnow, docstruct) 1.65 1.66 - with open(CACHE_HTML_FILE, 'w') as cache_html_file: 1.67 - cache_html_file.write(ret) 1.68 + with open(CACHE_HTML_FILE, 'w') as cache_html_file: 1.69 + cache_html_file.write(ret) 1.70 1.71 return ret 1.72 1.73 @@ -196,12 +196,13 @@ 1.74 def __init__(self): 1.75 self._iq = Queue.Queue(MAX_THREADS) 1.76 self._oq = Queue.Queue(MAX_THREADS) 1.77 + self._main_lock = threading.Lock() 1.78 1.79 for _ in range(MAX_THREADS): 1.80 WorkerThread(input_queue=self._iq, output_queue=self._oq).start() 1.81 1.82 def __call__(self, environ, start_response): 1.83 - response_body = main(self._iq, self._oq) 1.84 + response_body = main(self._iq, self._oq, self._main_lock) 1.85 response_headers = [ 1.86 ("Content-Type", "text/html"), 1.87 ("Content-Length", str(len(response_body))), 1.88 @@ -209,5 +210,3 @@ 1.89 start_response("200 OK", response_headers) 1.90 1.91 return [response_body] 1.92 - 1.93 -