Skip to content

Commit

Permalink
gracefully handle pickle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
c0fec0de committed Jan 18, 2018
1 parent f835fe5 commit a52e347
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions anycache/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,19 +387,22 @@ def __write(logger, ce, result, deps):
logger.info("WRITING cache entry '%s'" % (ce.ident))
# we need to lock the cache for write
# writing takes a long time, so we are writing to temporay files, lock and copy over.
with tempfile.NamedTemporaryFile("wb", prefix="anycache-", suffix=_CACHE_SUFFIX) as datatmpfile:
with tempfile.NamedTemporaryFile("w", prefix="anycache-", suffix=_DEP_SUFFIX) as deptmpfile:
# data
pickle.dump(result, datatmpfile)
datatmpfile.flush()
# dep
for dep in deps:
deptmpfile.write("%s\n" % (dep))
deptmpfile.flush()
# copy over
with ce.lock:
shutil.copyfile(datatmpfile.name, str(ce.data))
shutil.copyfile(deptmpfile.name, str(ce.dep))
try:
with tempfile.NamedTemporaryFile("wb", prefix="anycache-", suffix=_CACHE_SUFFIX) as datatmpfile:
with tempfile.NamedTemporaryFile("w", prefix="anycache-", suffix=_DEP_SUFFIX) as deptmpfile:
# data
pickle.dump(result, datatmpfile)
datatmpfile.flush()
# dep
for dep in deps:
deptmpfile.write("%s\n" % (dep))
deptmpfile.flush()
# copy over
with ce.lock:
shutil.copyfile(datatmpfile.name, str(ce.data))
shutil.copyfile(deptmpfile.name, str(ce.dep))
except Exception as exc:
logger.warn("FAILED cache write '%s'" % (ce.ident))

@staticmethod
def __tidyup(logger, cachedir, maxsize):
Expand Down

0 comments on commit a52e347

Please sign in to comment.