You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.8 KiB

8 months ago
################ 待整理
8 months ago
'''
8 months ago
多线程各个模块比较乱的但是协作序贯的完成了数据处理
各个组件完全不能互操作仅依靠队列发消息进行协作
适合环节多数据可分块有IO-计算性能设计考量要求让各个模块自己适应调整
在某些情况下可以避免复杂的控制流设计使代码简洁
8 months ago
'''
9 months ago
from threading import Thread
from queue import Queue
from cppy.cp_util import *
8 months ago
class ThreadObject(Thread):
9 months ago
def __init__(self):
super().__init__()
self.queue = Queue()
8 months ago
self._over = False
9 months ago
self.start()
def run(self):
8 months ago
while not self._over:
9 months ago
message = self.queue.get()
self._dispatch(message)
8 months ago
if message[0] == 'over':
break
9 months ago
def send(receiver, message):
receiver.queue.put(message)
8 months ago
class TxtManager(ThreadObject):
9 months ago
_data = ''
def _dispatch(self, message):
if message[0] == 'init':
self._init(message[1:])
elif message[0] == 'send_word_freqs':
self._process_words(message[1:])
8 months ago
else:
9 months ago
send(self._stop_word_manager, message)
def _init(self, message):
8 months ago
self._data = extract_file_words(message[0])
self._stop_word_manager = message[1]
9 months ago
def _process_words(self, message):
recipient = message[0]
for w in self._data:
send(self._stop_word_manager, ['filter', w])
8 months ago
send(self._stop_word_manager, ['topWord', recipient])
9 months ago
8 months ago
class FilterManager(ThreadObject):
9 months ago
_stop_words = []
def _dispatch(self, message):
if message[0] == 'init':
self._init(message[1:])
elif message[0] == 'filter':
return self._filter(message[1:])
8 months ago
else:
9 months ago
send(self._word_freqs_manager, message)
def _init(self, message):
self._stop_words = get_stopwords()
self._word_freqs_manager = message[0]
def _filter(self, message):
word = message[0]
if word not in self._stop_words:
send(self._word_freqs_manager, ['word', word])
8 months ago
class WFManager(ThreadObject):
9 months ago
_word_freqs = {}
def _dispatch(self, message):
if message[0] == 'word':
self._increment_count(message[1:])
8 months ago
elif message[0] == 'topWord':
self._topWord(message[1:])
9 months ago
def _increment_count(self, message):
word, = message
self._word_freqs[word] = self._word_freqs.get(word, 0) + 1
8 months ago
def _topWord(self, message):
9 months ago
recipient = message[0]
freqs_sorted = sort_dict ( self._word_freqs )
8 months ago
send(recipient, ['topWord', freqs_sorted])
9 months ago
8 months ago
class MyController(ThreadObject):
9 months ago
def _dispatch(self, message):
if message[0] == 'run':
self._run(message[1:])
8 months ago
elif message[0] == 'topWord':
9 months ago
self._display(message[1:])
else:
raise Exception("Message not understood " + message[0])
def _run(self, message):
self._storage_manager, = message
send(self._storage_manager, ['send_word_freqs', self])
def _display(self, message):
word_freqs, = message
print_word_freqs( word_freqs)
8 months ago
send(self._storage_manager, ['over'])
8 months ago
self._over = True
9 months ago
8 months ago
if __name__ == '__main__':
8 months ago
word_freq_manager = WFManager()
stop_word_manager = FilterManager()
storage_manager = TxtManager()
8 months ago
wfcontroller = MyController()
9 months ago
send(storage_manager, ['init', testfilepath, stop_word_manager])
8 months ago
send(stop_word_manager, ['init', word_freq_manager])
9 months ago
send(wfcontroller, ['run', storage_manager])
8 months ago
# 等待所有管理器完成工作
threads = [word_freq_manager, stop_word_manager, storage_manager, wfcontroller]
for thread in threads: thread.join()