From 4332d08292339b0a482c34ed2431f32fb8332465 Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Tue, 12 Jan 2016 09:50:15 +0100 Subject: [PATCH] Cleaned multi threading --- resources/lib/librarysync.py | 39 +++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/resources/lib/librarysync.py b/resources/lib/librarysync.py index feb841e8..7a8a51a4 100644 --- a/resources/lib/librarysync.py +++ b/resources/lib/librarysync.py @@ -56,13 +56,16 @@ class ThreadedGetMetadata(threading.Thread): try: updateItem = self.queue.get(block=False) # Empty queue - except: + except Queue.Empty: continue # Download Metadata - plexElement = plx.GetPlexMetadata(updateItem['itemId']) - updateItem['XML'] = plexElement + try: + updateItem['XML'] = plx.GetPlexMetadata(updateItem['itemId']) + except: + raise # place item into out queue self.out_queue.put(updateItem) + del updateItem # Keep track of where we are at with self.lock: getMetadataCount += 1 @@ -108,7 +111,7 @@ class ThreadedProcessMetadata(threading.Thread): try: updateItem = self.queue.get(block=False) # Empty queue - except: + except Queue.Empty: continue # Do the work; lock to be sure we've only got 1 Thread plexitem = updateItem['XML'] @@ -118,14 +121,17 @@ class ThreadedProcessMetadata(threading.Thread): title = updateItem['title'] itemSubFkt = getattr(item, method) with self.lock: - itemSubFkt( - plexitem, - viewtag=viewName, - viewid=viewId - ) - # Keep track of where we are at - processMetadataCount += 1 - processingViewName = title + try: + itemSubFkt( + plexitem, + viewtag=viewName, + viewid=viewId + ) + # Keep track of where we are at + processMetadataCount += 1 + processingViewName = title + except: + raise # signals to queue job is done self.queue.task_done() @@ -629,8 +635,7 @@ class LibrarySync(threading.Thread): 'method': method, 'viewName': viewName, 'viewId': viewId}) - # Update the Kodi popup info - return self.updatelist, self.allPlexElementsId + return def GetAndProcessXMLs(self, itemType): """ @@ -651,7 +656,7 @@ class LibrarySync(threading.Thread): self.logMsg("Starting sync threads", 1) self.logMsg("=====================", 1) getMetadataQueue = Queue.Queue() - processMetadataQueue = Queue.Queue() + processMetadataQueue = Queue.Queue(maxsize=100) getMetadataLock = threading.Lock() processMetadataLock = threading.Lock() # To keep track @@ -710,7 +715,9 @@ class LibrarySync(threading.Thread): self.logMsg("Stop sent to all threads", 1) # Wait till threads are indeed dead for thread in threads: - thread.join() + thread.join(15.0) + if thread.isAlive(): + self.logMsg("Could not terminate thread", -1) if dialog: dialog.close() self.logMsg("=====================", 1)