Я пытаюсь прочитать текстовый файл размером 400 МБ и читаю его по частям, так как мне нужно извлечь слова из него, и я пытаюсь использовать пул потоков, но он работает дольше, чем один process.
Ниже приведены две функции,
def process_text(self): # processing the text file
# if valid path then enter into try statement
try:
with open(self.path, 'r') as f:
pool = ThreadPool(20) # creating a thread pool of size 20
print("Processing text file ...")
while True:
data = list(islice(f, 100)) # slicing input file in size of 100 lines
if not data: break
pool.map(self.word_counting, data) #calling word_counting function which is extracting the words and storing the words in a dictionary
pool.close()
pool.join()
def word_counting(self, cur_list):
for line in cur_list:
for word in re.findall(r'\w{2,}', line):# will check for word of length greater than 1 i.e >1
self.word_dic[word] = self.word_dic.get(word, 0) + 1
Может ли кто-нибудь помочь с этим?