From d9ff0f84bd4db8a945d0e731046ec827f9dcedc8 Mon Sep 17 00:00:00 2001 From: Crista Lopes Date: Thu, 17 Oct 2013 21:14:17 -0700 Subject: [PATCH] Better reactive -- lock properly, remove unused arguments, send "end" to deamon thread --- 32-reactive/tf-32.py | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/32-reactive/tf-32.py b/32-reactive/tf-32.py index cb34612..5db435e 100644 --- a/32-reactive/tf-32.py +++ b/32-reactive/tf-32.py @@ -4,6 +4,8 @@ import sys, operator, string, os, threading from util import getch, cls from time import sleep +lock = threading.Lock() + # # The reactive infrastructure # @@ -11,19 +13,25 @@ class FreqObserver(threading.Thread): def __init__(self, freqs): threading.Thread.__init__(self) self.daemon = True + self._end = False # freqs is the data to be observed and reacted to self._freqs = freqs self._freqs_0 = sorted(self._freqs.iteritems(), key=operator.itemgetter(1), reverse=True) self.start() def run(self): - while True: + while not self._end: + lock.acquire() freqs_1 = sorted(self._freqs.iteritems(), key=operator.itemgetter(1), reverse=True) + lock.release() if (freqs_1[0:25] != self._freqs_0[0:25]): self._update_display(freqs_1[0:25]) self._freqs_0 = freqs_1 sleep(0.01) + def stop(): + self._end = True + def _update_display(self, tuples): def refresh_screen(data): # clear screen @@ -54,21 +62,19 @@ def get_input(): return True else: pass -f = open(sys.argv[1]) - -def characters(filename): +def characters(): c = f.read(1) if c != "": yield c else: raise StopIteration() -def all_words(filename): +def all_words(): found_word = False start_char = True while not found_word: try: - c = characters(filename).next() + c = characters().next() except StopIteration: raise StopIteration() @@ -87,22 +93,26 @@ def all_words(filename): found_word = True yield word -def non_stop_words(filename): +def non_stop_words(): stopwords = set(open('../stop_words.txt').read().split(',') + list(string.ascii_lowercase)) while True: - w = all_words(filename).next() + w = all_words().next() if not w in stopwords: yield w -def count_and_sort(filename): +def count_and_sort(): freqs = {} # The declaration for reactive observation of freqs - FreqObserver(freqs) + observer = FreqObserver(freqs) while get_input(): try: - w = non_stop_words(filename).next() + w = non_stop_words().next() + lock.acquire() freqs[w] = 1 if w not in freqs else freqs[w]+1 + lock.release() except StopIteration: + # Let's wait for the observer thread to die gracefully + sleep(1) break # @@ -110,6 +120,6 @@ def count_and_sort(filename): # print "Press space bar to fetch words from the file one by one" print "Press ESC to switch to automatic mode" -count_and_sort(sys.argv[1]) - +with open(sys.argv[1])as f: + count_and_sort()