我编写了一个例程来浏览一堆文件,并根据这些文件每行中找到的数据添加数据库条目(如果还没有)。在for root, dirs, files in walk_dir(path):
for file_name in files:
file_path = join_path(root, file_name)
with transaction.commit_on_success():
with open(file_path, "r") as f:
for i, line in enumerate(f):
handle_line(line)
def handle_line(line):
phrase, translated_phrase, context = get_params(line)
try:
ph = Phrase.objects.get(name=phrase)
except Phrase.DoesNotExist:
ph = Phrase(name=phrase)
ph.save()
try:
tr = Translation.object.get(phrase=ph, name=translated_phrase)
except Translation.DoesNotExist:
tr = Translation(phrase=ph, name=translated_phrase)
tr.save()
try:
tm = TMTranslation.object.get(translation=tr, context=context)
except TMTranslation.DoesNotExist:
tm = TMTranslation(translation=tr, context=context)
tm.save()
可能需要处理很多数据(可能是1000个文件,每个文件中有1000行)。但我还是不明白为什么我总是遇到记忆问题。不应该至少在每个文件之后(在每个事务之后)释放内存吗?在
但我所经历的是,这个过程会慢慢耗尽我所有的内存,并开始耗尽交换空间。我做错什么了?在
651

被折叠的 条评论
为什么被折叠?



