本文共 932 字,大约阅读时间需要 3 分钟。
这里有一个对你有用的解决方案。我改变了你的很多代码,所以如果你有任何问题请问。在
这远远不是实现这一点的唯一方法,尤其是这不是一个节省内存的解决方案。在
你需要给max_workers设置一个对你有用的东西。通常,计算机中逻辑处理器的数量是一个很好的起点。在from concurrent.futures import ThreadPoolExecutor, Future
from itertools import permutations
from collections import namedtuple, defaultdict
Result = namedtuple('Result', ('value', 'word'))
def new_calculate_similarity(word1, word2):
return Result(
calculate_similarity(global_map[word1], global_map[word2]),
word2)
with ThreadPoolExecutor(max_workers=4) as executer:
futures = defaultdict(list)
for word1, word2 in permutations(unique_words, r=2):
futures[word1].append(
executer.submit(new_calculate_similarity, word1, word2))
for word in futures:
# this will block until all calculations have completed for 'word'
results = map(Future.result, futures[word])
max_result = max(results, key=lambda r: r.value)
print(word, max_result.word, max_result.value,
sep='\t',
file=file_co_occurring)
以下是我使用的库的文档:
转载地址:https://blog.csdn.net/weixin_34007963/article/details/114429579 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!