remove timeout support, it's not needed
This commit is contained in:
parent
989ce70f97
commit
5d5e2b199c
@ -8,26 +8,21 @@ from contextlib import contextmanager
|
|||||||
class FileDict:
|
class FileDict:
|
||||||
"""Concurrency-safe multi-reader-single-writer Persistent Dict."""
|
"""Concurrency-safe multi-reader-single-writer Persistent Dict."""
|
||||||
|
|
||||||
def __init__(self, path, timeout=5.0):
|
def __init__(self, path):
|
||||||
self.path = path
|
self.path = path
|
||||||
self.lock_path = path.with_name(path.name + ".lock")
|
self.lock_path = path.with_name(path.name + ".lock")
|
||||||
self.timeout = timeout
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def modify(self):
|
def modify(self):
|
||||||
try:
|
# the OS will release the lock if the process dies,
|
||||||
with filelock.FileLock(self.lock_path, timeout=self.timeout):
|
# and the contextmanager will otherwise guarantee release
|
||||||
|
with filelock.FileLock(self.lock_path):
|
||||||
data = self.read()
|
data = self.read()
|
||||||
yield data
|
yield data
|
||||||
write_path = self.path.with_suffix(".tmp")
|
write_path = self.path.with_suffix(".tmp")
|
||||||
with write_path.open("wb") as f:
|
with write_path.open("wb") as f:
|
||||||
marshal.dump(data, f)
|
marshal.dump(data, f)
|
||||||
os.rename(write_path, self.path)
|
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
|
|
||||||
|
|
||||||
def read(self):
|
def read(self):
|
||||||
try:
|
try:
|
||||||
|
@ -12,18 +12,6 @@ def test_basic(tmp_path):
|
|||||||
assert new["456"] == 4.2
|
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):
|
def test_bad_marshal_file(tmp_path, caplog):
|
||||||
fdict1 = FileDict(tmp_path.joinpath("metadata"))
|
fdict1 = FileDict(tmp_path.joinpath("metadata"))
|
||||||
fdict1.path.write_bytes(b"l12k3l12k3l")
|
fdict1.path.write_bytes(b"l12k3l12k3l")
|
||||||
|
Loading…
Reference in New Issue
Block a user