diff --git a/语言特性/异步/82.py b/语言特性/异步/82.py new file mode 100644 index 0000000..f0caa61 --- /dev/null +++ b/语言特性/异步/82.py @@ -0,0 +1,27 @@ +import asyncio +import aiofiles +from collections import Counter +from cppy.cp_util import * + +async def read_file(file_path): + async with aiofiles.open(file_path, 'r', encoding='utf-8') as file: + content = await file.read() + return content + +async def count_words(text): + words = extract_str_words(text.lower()) + word_counts = Counter(words) + return word_counts + +async def main(): + wordfreqs = Counter() + files = [ testfilepath ] * 10 + for thisfile in files: + text = await read_file( thisfile ) + top_words = await count_words(text) + wordfreqs += top_words + for word, count in wordfreqs.most_common(10): + print(f"{word}: {count//10}") + +# 运行异步主函数 +asyncio.run(main()) \ No newline at end of file diff --git a/语言特性/异步/tf_91.py b/语言特性/异步/tf_91.py index 14eb398..9ec04c1 100644 --- a/语言特性/异步/tf_91.py +++ b/语言特性/异步/tf_91.py @@ -2,10 +2,6 @@ import threading from collections import Counter from cppy.cp_util import * -""" - 把切分数据片段分给多线程改为分配多个文件给多个线程(有IO操作),就能看到效果了 -""" - stop_words = get_stopwords() # 定义一个函数来计算每个线程的词频