# HG changeset patch
# User paulo
# Date 1360054657 28800
# Node ID c673e9e9c4ca582073a1fec31b03133bd955dae0
# Parent  df6a1a34758444d111acf579097de6a7940bad5b
add threading.Lock to main() to support threaded server

diff -r df6a1a347584 -r c673e9e9c4ca myrss/myrss_app.py
--- a/myrss/myrss_app.py	Tue Feb 05 00:01:49 2013 -0800
+++ b/myrss/myrss_app.py	Tue Feb 05 00:57:37 2013 -0800
@@ -16,8 +16,7 @@
 FEEDS_FILE = "FEEDS"
 CACHE_HTML_FILE = "__cache__.html"
 
-#CACHE_LIFE = 1200 # [seconds]
-CACHE_LIFE = 30 # [seconds]
+CACHE_LIFE = 1200 # [seconds]
 MAX_ITEMS = 30
 MAX_LINK_Z = 4
 MAX_THREADS = 20
@@ -158,36 +157,37 @@
 			self._output_queue.put((idx, docfeed))
 			
 
-def main(input_queue, output_queue):
+def main(input_queue, output_queue, lock):
 	ret = ''
 
-	epoch_now = time.time()
-	dtnow = datetime.datetime.fromtimestamp(epoch_now)
+	with lock:
+		epoch_now = time.time()
+		dtnow = datetime.datetime.fromtimestamp(epoch_now)
 
-	if os.path.exists(CACHE_HTML_FILE) and (epoch_now - os.stat(CACHE_HTML_FILE).st_mtime) < float(CACHE_LIFE):
-		with open(CACHE_HTML_FILE) as cache_html_file:
-			ret = cache_html_file.read()
+		if os.path.exists(CACHE_HTML_FILE) and (epoch_now - os.stat(CACHE_HTML_FILE).st_mtime) < float(CACHE_LIFE):
+			with open(CACHE_HTML_FILE) as cache_html_file:
+				ret = cache_html_file.read()
 
-	else:
-		with open(FEEDS_FILE) as feeds_file:
-			feedlines = feeds_file.readlines()
+		else:
+			with open(FEEDS_FILE) as feeds_file:
+				feedlines = feeds_file.readlines()
 
-		docstruct = [None]*len(feedlines)
-		num_input = 0
-		for (i, l) in enumerate(feedlines):
-			if l[0] != '#':
-				l = l.strip()
-				input_queue.put((i, l))
-				num_input += 1
+			docstruct = [None]*len(feedlines)
+			num_input = 0
+			for (i, l) in enumerate(feedlines):
+				if l[0] != '#':
+					l = l.strip()
+					input_queue.put((i, l))
+					num_input += 1
 
-		for _ in range(num_input):
-			(idx, docfeed) = output_queue.get()
-			docstruct[idx] = docfeed
+			for _ in range(num_input):
+				(idx, docfeed) = output_queue.get()
+				docstruct[idx] = docfeed
 
-		ret = _to_html(dtnow, docstruct)
+			ret = _to_html(dtnow, docstruct)
 
-		with open(CACHE_HTML_FILE, 'w') as cache_html_file:
-			cache_html_file.write(ret)
+			with open(CACHE_HTML_FILE, 'w') as cache_html_file:
+				cache_html_file.write(ret)
 
 	return ret
 
@@ -196,12 +196,13 @@
 	def __init__(self):
 		self._iq = Queue.Queue(MAX_THREADS)
 		self._oq = Queue.Queue(MAX_THREADS)
+		self._main_lock = threading.Lock()
 
 		for _ in range(MAX_THREADS):
 			WorkerThread(input_queue=self._iq, output_queue=self._oq).start()
 
 	def __call__(self, environ, start_response):
-		response_body = main(self._iq, self._oq)
+		response_body = main(self._iq, self._oq, self._main_lock)
 		response_headers = [
 			("Content-Type", "text/html"),
 			("Content-Length", str(len(response_body))),
@@ -209,5 +210,3 @@
 		start_response("200 OK", response_headers)
 
 		return [response_body]
-
-