comparison 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
comparison
equal deleted inserted replaced
0:ab068cab2149 1:d73fc7405ce6
14 14
15 15
16 FEEDS_FILE = "FEEDS" 16 FEEDS_FILE = "FEEDS"
17 CACHE_HTML_FILE = "__cache__.html" 17 CACHE_HTML_FILE = "__cache__.html"
18 18
19 #CACHE_LIFE = 1200 # [seconds] 19 CACHE_LIFE = 1200 # [seconds]
20 CACHE_LIFE = 30 # [seconds]
21 MAX_ITEMS = 30 20 MAX_ITEMS = 30
22 MAX_LINK_Z = 4 21 MAX_LINK_Z = 4
23 MAX_THREADS = 20 22 MAX_THREADS = 20
24 23
25 24
156 except Exception as e: 155 except Exception as e:
157 logging.info("(%s) exception: %s" % (url, e)) 156 logging.info("(%s) exception: %s" % (url, e))
158 self._output_queue.put((idx, docfeed)) 157 self._output_queue.put((idx, docfeed))
159 158
160 159
161 def main(input_queue, output_queue): 160 def main(input_queue, output_queue, lock):
162 ret = '' 161 ret = ''
163 162
164 epoch_now = time.time() 163 with lock:
165 dtnow = datetime.datetime.fromtimestamp(epoch_now) 164 epoch_now = time.time()
166 165 dtnow = datetime.datetime.fromtimestamp(epoch_now)
167 if os.path.exists(CACHE_HTML_FILE) and (epoch_now - os.stat(CACHE_HTML_FILE).st_mtime) < float(CACHE_LIFE): 166
168 with open(CACHE_HTML_FILE) as cache_html_file: 167 if os.path.exists(CACHE_HTML_FILE) and (epoch_now - os.stat(CACHE_HTML_FILE).st_mtime) < float(CACHE_LIFE):
169 ret = cache_html_file.read() 168 with open(CACHE_HTML_FILE) as cache_html_file:
170 169 ret = cache_html_file.read()
171 else: 170
172 with open(FEEDS_FILE) as feeds_file: 171 else:
173 feedlines = feeds_file.readlines() 172 with open(FEEDS_FILE) as feeds_file:
174 173 feedlines = feeds_file.readlines()
175 docstruct = [None]*len(feedlines) 174
176 num_input = 0 175 docstruct = [None]*len(feedlines)
177 for (i, l) in enumerate(feedlines): 176 num_input = 0
178 if l[0] != '#': 177 for (i, l) in enumerate(feedlines):
179 l = l.strip() 178 if l[0] != '#':
180 input_queue.put((i, l)) 179 l = l.strip()
181 num_input += 1 180 input_queue.put((i, l))
182 181 num_input += 1
183 for _ in range(num_input): 182
184 (idx, docfeed) = output_queue.get() 183 for _ in range(num_input):
185 docstruct[idx] = docfeed 184 (idx, docfeed) = output_queue.get()
186 185 docstruct[idx] = docfeed
187 ret = _to_html(dtnow, docstruct) 186
188 187 ret = _to_html(dtnow, docstruct)
189 with open(CACHE_HTML_FILE, 'w') as cache_html_file: 188
190 cache_html_file.write(ret) 189 with open(CACHE_HTML_FILE, 'w') as cache_html_file:
190 cache_html_file.write(ret)
191 191
192 return ret 192 return ret
193 193
194 194
195 class MyRssApp: 195 class MyRssApp:
196 def __init__(self): 196 def __init__(self):
197 self._iq = Queue.Queue(MAX_THREADS) 197 self._iq = Queue.Queue(MAX_THREADS)
198 self._oq = Queue.Queue(MAX_THREADS) 198 self._oq = Queue.Queue(MAX_THREADS)
199 self._main_lock = threading.Lock()
199 200
200 for _ in range(MAX_THREADS): 201 for _ in range(MAX_THREADS):
201 WorkerThread(input_queue=self._iq, output_queue=self._oq).start() 202 WorkerThread(input_queue=self._iq, output_queue=self._oq).start()
202 203
203 def __call__(self, environ, start_response): 204 def __call__(self, environ, start_response):
204 response_body = main(self._iq, self._oq) 205 response_body = main(self._iq, self._oq, self._main_lock)
205 response_headers = [ 206 response_headers = [
206 ("Content-Type", "text/html"), 207 ("Content-Type", "text/html"),
207 ("Content-Length", str(len(response_body))), 208 ("Content-Length", str(len(response_body))),
208 ] 209 ]
209 start_response("200 OK", response_headers) 210 start_response("200 OK", response_headers)
210 211
211 return [response_body] 212 return [response_body]
212
213