【发布时间】:2019-11-04 22:36:06
【问题描述】:
在摄取大量数据之前,我的多处理池(8 核,16 GB RAM)正在使用我的所有内存。我在一个 6 GB 的数据集上运行
我尝试过使用各种类型的处理器,包括 imap、imap_unordered、apply、map 等。我也尝试过 maxtasksperchild,这似乎增加了内存使用量。
import string
import re
import multiprocessing as mp
from tqdm import tqdm
linkregex = re.compile(r"http\S+")
puncregex = re.compile(r"(?<=\w)[^\s\w](?![^\s\w])")
emojiregex = re.compile(r"(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])")
sentences = []
def process(item):
return re.sub(emojiregex, r" \1 ", re.sub(puncregex,"",re.sub(linkregex, "link", item))).lower().split()
if __name__ == '__main__':
with mp.Pool(8) as pool:
sentences = list(tqdm(pool.imap_unordered(process, open('scrape/output.txt')
), total=52123146))
print(str(len(sentences)))
with open("final/word2vectweets.txt", "a+") as out:
out.write(sentences)
这应该从文件中返回已处理行的列表,但它消耗内存太快。以前没有mp和处理简单的版本已经成功了。
【问题讨论】:
-
你确定
imap_unordered在给定文件对象时会这样工作吗?似乎您从不关闭该文件或任何东西。作为一般规则,我使用with open() as :所有地方,我怎么推荐都不够。 -
其他一些注意事项:我相信在打印
len(sentences)时调用str是不必要的。打开输出文件时a+模式也是如此,只需a就足够了。我现在无法使用计算机,所以我将测试所有内容并在明天写出正确的答案:)
标签: python memory multiprocessing pool tqdm