Skip to content

Commit

Permalink
[ui] thumbnail cache: use thread pool to manage thread lifetime autom…
Browse files Browse the repository at this point in the history
…atically
  • Loading branch information
mugulmd committed Feb 9, 2023
1 parent db8d00e commit c758bf0
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions meshroom/ui/components/thumbnail.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import time
import logging
from threading import Thread
from multiprocessing.pool import ThreadPool


class ThumbnailCache(QObject):
Expand Down Expand Up @@ -57,10 +58,9 @@ class ThumbnailCache(QObject):
thumbnailCreated = Signal(QUrl, int)

# Threads info and LIFO structure for running clean and createThumbnail asynchronously
maxWorkerThreads = 3
activeWorkerThreads = 0
requests = []
cleaningThread = None
workerThreads = ThreadPool(processes=3)

@staticmethod
def initialize():
Expand Down Expand Up @@ -211,11 +211,9 @@ def thumbnail(self, imgSource, callerID):
return source

# Thumbnail does not exist
# Create request and start a thread if needed
# Create request and submit to worker threads
ThumbnailCache.requests.append((imgSource, callerID))
if ThumbnailCache.activeWorkerThreads < ThumbnailCache.maxWorkerThreads:
thread = Thread(target=self.handleRequestsAsync)
thread.start()
ThumbnailCache.workerThreads.apply_async(func=self.handleRequestsAsync)

return None

Expand Down Expand Up @@ -264,15 +262,9 @@ def createThumbnail(self, imgSource, callerID):
def handleRequestsAsync(self):
"""Process thumbnail creation requests in LIFO order.
This method is only meant to be called by worker threads,
hence it also takes care of registering/unregistering the calling thread.
Note: this operation waits for the cleaning process to finish before starting,
in order to avoid synchronization issues.
"""
# Register worker thread
ThumbnailCache.activeWorkerThreads += 1

# Wait for cleaning thread to finish
if ThumbnailCache.cleaningThread is not None and ThumbnailCache.cleaningThread.is_alive():
ThumbnailCache.cleaningThread.join()
Expand All @@ -284,8 +276,7 @@ def handleRequestsAsync(self):
self.createThumbnail(req[0], req[1])
except IndexError:
# No more request to process
# Unregister worker thread
ThumbnailCache.activeWorkerThreads -= 1
return

@Slot()
def clearRequests(self):
Expand Down

0 comments on commit c758bf0

Please sign in to comment.