MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / _fill_cache_start_reader

Method _fill_cache_start_reader

monai/data/dataset.py:634–695  ·  view source on GitHub ↗

Check the LMDB cache and write the cache if needed. py-lmdb doesn't have a good support for concurrent write. This method can be used with multiple processes, but it may have a negative impact on the performance. Args: show_progress: whether to show the progress

(self, show_progress=True)

Source from the content-addressed store, hash-verified

632 return torch.load(BytesIO(val), map_location="cpu", weights_only=True)
633
634 def _fill_cache_start_reader(self, show_progress=True):
635 """
636 Check the LMDB cache and write the cache if needed. py-lmdb doesn't have a good support for concurrent write.
637 This method can be used with multiple processes, but it may have a negative impact on the performance.
638
639 Args:
640 show_progress: whether to show the progress bar if possible.
641 """
642 # Close any open read environment before attempting write-mode access
643 # to prevent "environment already open" errors when multiple LMDBDataset
644 # instances target the same db file
645 if self._read_env is not None:
646 self._read_env.close()
647 self._read_env = None
648 # create cache
649 self.lmdb_kwargs["readonly"] = False
650 env = lmdb.open(path=f"{self.db_file}", subdir=False, **self.lmdb_kwargs)
651 if show_progress and not has_tqdm:
652 warnings.warn("LMDBDataset: tqdm is not installed. not displaying the caching progress.")
653 with env.begin(write=False) as search_txn:
654 for item in tqdm(self.data) if has_tqdm and show_progress else self.data:
655 key = self.hash_func(item)
656 done, retry, val = False, 5, None
657 while not done and retry > 0:
658 try:
659 with search_txn.cursor() as cursor:
660 done = cursor.set_key(key)
661 if done:
662 continue
663 if val is None:
664 val = self._pre_transform(deepcopy(item)) # keep the original hashed
665 # val = pickle.dumps(val, protocol=self.pickle_protocol)
666 val = self._safe_serialize(val)
667 with env.begin(write=True) as txn:
668 txn.put(key, val)
669 done = True
670 except lmdb.MapFullError:
671 done, retry = False, retry - 1
672 size = env.info()["map_size"]
673 new_size = size * 2
674 warnings.warn(
675 f"Resizing the cache database from {int(size) >> 20}MB to {int(new_size) >> 20}MB."
676 )
677 env.set_mapsize(new_size)
678 except lmdb.MapResizedError:
679 # the mapsize is increased by another process
680 # set_mapsize with a size of 0 to adopt the new size
681 env.set_mapsize(0)
682 if not done: # still has the map full error
683 size = env.info()["map_size"]
684 env.close()
685 raise ValueError(f"LMDB map size reached, increase size above current size of {size}.")
686 size = env.info()["map_size"]
687 env.close()
688 # read-only database env
689 self.lmdb_kwargs["readonly"] = True
690 self.lmdb_kwargs["map_size"] = size
691 if self.lmdb_kwargs.get("lock", None) is None:

Callers 4

__init__Method · 0.95
set_dataMethod · 0.95
_cachecheckMethod · 0.95
infoMethod · 0.95

Calls 6

_safe_serializeMethod · 0.95
tqdmFunction · 0.85
infoMethod · 0.80
getMethod · 0.80
closeMethod · 0.45
_pre_transformMethod · 0.45

Tested by

no test coverage detected