From 3f96778f2aaa66a59b7b46ae4aee3569e7b53acb Mon Sep 17 00:00:00 2001 From: mugulmd Date: Tue, 17 Jan 2023 03:09:39 -0800 Subject: [PATCH] [ui] ThumbnailCache: check if thumbnail has been created by another thread --- meshroom/ui/components/thumbnail.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/meshroom/ui/components/thumbnail.py b/meshroom/ui/components/thumbnail.py index 4575d3c2..c809f01d 100644 --- a/meshroom/ui/components/thumbnail.py +++ b/meshroom/ui/components/thumbnail.py @@ -165,6 +165,22 @@ class ThumbnailCache(QObject): path = os.path.join(ThumbnailCache.thumbnailDir, f'{digest}.jpg') return path + @staticmethod + def checkThumbnail(path): + """Check if a thumbnail already exists on disk, and if so update its last modification time. + + Args: + path (str): filepath to the thumbnail + + Returns: + (bool): whether the thumbnail exists on disk or not + """ + if os.path.exists(path): + # Update last modification time + Path(path).touch(exist_ok=True) + return True + return False + @Slot(QUrl, int, result=QUrl) def thumbnail(self, imgSource, callerID): """Retrieve the filepath of the thumbnail corresponding to a given image. @@ -187,9 +203,7 @@ class ThumbnailCache(QObject): source = QUrl.fromLocalFile(path) # Check if thumbnail already exists - if os.path.exists(path): - # Update last modification time - Path(path).touch(exist_ok=True) + if ThumbnailCache.checkThumbnail(path): return source # Thumbnail does not exist @@ -210,6 +224,12 @@ class ThumbnailCache(QObject): """ imgPath = imgSource.toLocalFile() path = ThumbnailCache.thumbnailPath(imgPath) + + # Check if thumbnail already exists (it may have been created by another thread) + if ThumbnailCache.checkThumbnail(path): + self.thumbnailCreated.emit(imgSource, callerID) + return + logging.debug(f'[ThumbnailCache] Creating thumbnail {path} for image {imgPath}') # Initialize image reader object