I'm working with a class called CachedRandomIterDataset that uses an asynchronous thread to load data into a cache from a dataset. The cache is supposed to be filled with data, shuffled, and then iterated over.However, I'm encountering an issue with cache state inconsistency, and I'm hoping someone can help me out. Thank you very much in advance for any assistance! When the cache is emptied by the iterator (all data has been popped out), the thread attempts to reload the cache. However, the thread sometimes incorrectly assumes that the cache is full, even though the cache is actually empty. This prevents the thread from refilling the cache, and the iterator, when attempting to pop data again, finds the cache empty and cannot proceed. Here is the implementation of the class: def _load_data(self): """Asynchronous function to load data into cache.""" while not self.stop_thread: self.lock.acquire() if len(self.cache) < self.cache_size // 2: try: cnt_iter = range(self.cache_size - len(self.cache)) if self.show_bar: cnt_iter = tqdm.tqdm(cnt_iter, desc="Buffering...") for _ in cnt_iter: self.cache.append(next(self.iterator)) print(time.ctime(), "load", self.cache_size, len(self.cache), id(self.cache)) random.shuffle(self.cache) except StopIteration: # print("stop") self.stop_thread = True else: print(time.ctime(), "load full", self.cache_size, len(self.cache), id(self.cache)) pass self.lock.release() time.sleep(1) def __iter__(self) -> torchio.Subject: while True: self.lock.acquire() print(time.ctime(), "iter", self.cache_size, len(self.cache), id(self.cache)) if self.cache: # If cache is not empty, yield item item = self.cache.pop() print("111") yield item else: print(time.ctime(), "cache empty", self.cache_size, len(self.cache), id(self.cache)) # Wait for data to be loaded into cache if self.stop_thread: break time.sleep(1) self.lock.release() How can I resolve the issue where the thread incorrectly assumes the cache is full? Is there a threading issue that causes the cache state check to be incorrect? Thank you again for your time and help! Continue reading...