【发布时间】:2018-04-11 13:38:03
【问题描述】:
问题的目的:详细了解在 Python / 实验中实现并发的方法。
上下文:我想计算所有文件中匹配特定模式的所有单词。这个想法是我可以调用函数count_words('/foo/bar/*.txt') 并且所有单词(即,由一个或多个空白字符分隔的字符串)都将被计算在内。
在实现中,我正在寻找使用并发实现count_words 的方法。到目前为止,我设法使用了multiprocessing 和asyncio。
您是否看到完成相同任务的替代方法?
我没有使用threading,因为我注意到由于 Python GIL 的限制,性能提升并没有那么令人印象深刻。
import asyncio
import multiprocessing
import time
from pathlib import Path
from pprint import pprint
def count_words(file):
with open(file) as f:
return sum(len(line.split()) for line in f)
async def count_words_for_file(file):
with open(file) as f:
return sum(len(line.split()) for line in f)
def async_count_words(path, glob_pattern):
event_loop = asyncio.get_event_loop()
try:
print("Entering event loop")
for file in list(path.glob(glob_pattern)):
result = event_loop.run_until_complete(count_words_for_file(file))
print(result)
finally:
event_loop.close()
def multiprocess_count_words(path, glob_pattern):
with multiprocessing.Pool(processes=8) as pool:
results = pool.map(count_words, list(path.glob(glob_pattern)))
pprint(results)
def sequential_count_words(path, glob_pattern):
for file in list(path.glob(glob_pattern)):
print(count_words(file))
if __name__ == '__main__':
benchmark = []
path = Path("../data/gutenberg/")
# no need for benchmark on sequential_count_words, it is very slow!
# sequential_count_words(path, "*.txt")
start = time.time()
async_count_words(path, "*.txt")
benchmark.append(("async version", time.time() - start))
start = time.time()
multiprocess_count_words(path, "*.txt")
benchmark.append(("multiprocess version", time.time() - start))
print(*benchmark)
为了模拟大量文件,我从 Project Gutenberg (http://gutenberg.org/) 下载了一些书籍,并使用以下命令创建了同一文件的多个副本。
for i in {000..99}; do cp 56943-0.txt $(openssl rand -base64 12)-$i.txt; done
【问题讨论】:
标签: python python-3.x concurrency