Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.

Commit 153bade

Browse files
committed
Unique file ids
1 parent b7d4a92 commit 153bade

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

allennlp/common/file_utils.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,13 @@ def _serialize(data):
567567
return np.frombuffer(buffer, dtype=np.uint8)
568568

569569

570-
_active_tensor_caches: MutableMapping[str, "TensorCache"] = weakref.WeakValueDictionary()
570+
_active_tensor_caches: MutableMapping[int, "TensorCache"] = weakref.WeakValueDictionary()
571+
572+
573+
def _unique_file_id(path: Union[str, PathLike]) -> int:
574+
result = os.stat(path).st_ino
575+
assert result != 0
576+
return result
571577

572578

573579
class TensorCache(MutableMapping[str, Tensor], ABC):
@@ -584,7 +590,10 @@ def __new__(cls, filename: Union[str, PathLike], *, read_only: bool = False, **k
584590
# This mechanism makes sure we re-use open lmdb file handles. Lmdb has a problem when the same file is
585591
# opened by the same process multiple times. This is our workaround.
586592
filename = str(filename)
587-
result = _active_tensor_caches.get(filename)
593+
try:
594+
result = _active_tensor_caches.get(_unique_file_id(filename))
595+
except FileNotFoundError:
596+
result = None
588597
if result is None:
589598
result = super(TensorCache, cls).__new__(
590599
cls, filename, read_only=read_only, **kwargs
@@ -691,7 +700,7 @@ def __init__(
691700
readonly=read_only,
692701
lock=use_lock,
693702
)
694-
_active_tensor_caches[self.lmdb_env.path()] = self
703+
_active_tensor_caches[_unique_file_id(filename)] = self
695704

696705
# We have another cache here that makes sure we return the same object for the same key. Without it,
697706
# you would get a different tensor, using different memory, every time you call __getitem__(), even

0 commit comments

Comments
 (0)