remove timeout support, it's not needed

This commit is contained in:
holger krekel 2024-03-27 18:32:40 +01:00
parent 989ce70f97
commit 5d5e2b199c
2 changed files with 10 additions and 27 deletions

View File

@ -8,26 +8,21 @@ from contextlib import contextmanager
class FileDict:
"""Concurrency-safe multi-reader-single-writer Persistent Dict."""
def __init__(self, path, timeout=5.0):
def __init__(self, path):
self.path = path
self.lock_path = path.with_name(path.name + ".lock")
self.timeout = timeout
@contextmanager
def modify(self):
try:
with filelock.FileLock(self.lock_path, timeout=self.timeout):
data = self.read()
yield data
write_path = self.path.with_suffix(".tmp")
with write_path.open("wb") as f:
marshal.dump(data, f)
os.rename(write_path, self.path)
except filelock.Timeout:
logging.warning("could not obtain lock, removing: %r", self.lock_path)
os.remove(self.lock_path)
with self.modify() as d:
yield d
# the OS will release the lock if the process dies,
# and the contextmanager will otherwise guarantee release
with filelock.FileLock(self.lock_path):
data = self.read()
yield data
write_path = self.path.with_suffix(".tmp")
with write_path.open("wb") as f:
marshal.dump(data, f)
os.rename(write_path, self.path)
def read(self):
try:

View File

@ -12,18 +12,6 @@ def test_basic(tmp_path):
assert new["456"] == 4.2
def test_dying_lock(tmp_path, caplog):
fdict1 = FileDict(tmp_path.joinpath("metadata"))
fdict2 = FileDict(tmp_path.joinpath("metadata"), timeout=0.1)
with fdict1.modify() as d:
with fdict2.modify() as d2:
d2["1"] = "2"
assert "could not obtain" in caplog.records[0].msg
d["1"] = "3"
assert fdict1.read()["1"] == "3"
assert fdict2.read()["1"] == "3"
def test_bad_marshal_file(tmp_path, caplog):
fdict1 = FileDict(tmp_path.joinpath("metadata"))
fdict1.path.write_bytes(b"l12k3l12k3l")