file kept open in utils pickled resource, fixed

This commit is contained in:
Alejandro Moreo Fernandez 2024-04-30 09:55:28 +02:00
parent 244d1045ce
commit 7f39f4df66
1 changed files with 5 additions and 3 deletions

View File

@ -228,11 +228,13 @@ def pickled_resource(pickle_path:str, generation_func:callable, *args):
return generation_func(*args)
else:
if os.path.exists(pickle_path):
return pickle.load(open(pickle_path, 'rb'))
with open(pickle_path, 'rb') as fin:
instance = pickle.load(fin)
else:
instance = generation_func(*args)
os.makedirs(str(Path(pickle_path).parent), exist_ok=True)
pickle.dump(instance, open(pickle_path, 'wb'), pickle.HIGHEST_PROTOCOL)
with open(pickle_path, 'wb') as foo:
pickle.dump(instance, foo, pickle.HIGHEST_PROTOCOL)
return instance