Revert "Revert "Dedicated art downloading queue""

This reverts commit 6621cc5a8d.
This commit is contained in:
tomkat83 2016-09-17 11:24:12 +02:00
parent c0d91507c0
commit cd14127233

View file

@ -8,7 +8,8 @@ import requests
import os import os
import urllib import urllib
from sqlite3 import OperationalError from sqlite3 import OperationalError
from threading import Lock from threading import Lock, Thread
import Queue
import xbmc import xbmc
import xbmcgui import xbmcgui
@ -16,7 +17,8 @@ import xbmcvfs
import image_cache_thread import image_cache_thread
from utils import window, settings, language as lang, kodiSQL, tryEncode, \ from utils import window, settings, language as lang, kodiSQL, tryEncode, \
tryDecode, IfExists tryDecode, IfExists, ThreadMethods, ThreadMethodsAdditionalSuspend, \
ThreadMethodsAdditionalStop
# Disable annoying requests warnings # Disable annoying requests warnings
import requests.packages.urllib3 import requests.packages.urllib3
@ -119,22 +121,78 @@ def setKodiWebServerDetails():
return (xbmc_port, xbmc_username, xbmc_password) return (xbmc_port, xbmc_username, xbmc_password)
class Artwork(): @ThreadMethodsAdditionalSuspend('suspend_LibraryThread')
lock = Lock() @ThreadMethodsAdditionalStop('plex_shouldStop')
@ThreadMethods
enableTextureCache = settings('enableTextureCache') == "true" class Image_Cache_Thread(Thread):
imageCacheLimitThreads = int(settings('imageCacheLimit')) imageCacheLimitThreads = int(settings('imageCacheLimit'))
imageCacheLimitThreads = imageCacheLimitThreads * 5 imageCacheLimitThreads = imageCacheLimitThreads * 5
log.info("Using Image Cache Thread Count: %s" % imageCacheLimitThreads) log.info("Using Image Cache Thread Count: %s" % imageCacheLimitThreads)
xbmc_port, xbmc_username, xbmc_password = setKodiWebServerDetails()
threads = []
xbmc_host = 'localhost' def __init__(self, queue):
xbmc_port = None self.queue = queue
xbmc_username = None Thread.__init__(self)
xbmc_password = None
def run(self):
threadStopped = self.threadStopped
threadSuspended = self.threadSuspended
queue = self.queue
threads = self.threads
imageCacheLimitThreads = self.imageCacheLimitThreads
log.info("---===### Starting Image_Cache_Thread ###===---")
while not threadStopped():
# In the event the server goes offline
while threadSuspended():
# Set in service.py
if threadStopped():
# Abort was requested while waiting. We should exit
log.info("---===### Stopped Image_Cache_Thread ###===---")
return
xbmc.sleep(1000)
try:
url = queue.get(block=False)
except Queue.Empty:
xbmc.sleep(200)
continue
while True:
for thread in threads:
if thread.isAlive() is False:
threads.remove(thread)
if len(threads) < imageCacheLimitThreads:
log.debug('Downloading %s' % url)
thread = Thread(target=self.download,
args=(url,))
thread.setDaemon(True)
thread.start()
threads.append(thread)
break
log.info("---===### Stopped Image_Cache_Thread ###===---")
def download(self):
try:
requests.head(
url=("http://%s:%s/image/image://%s"
% (self.xbmc_host, self.xbmc_port, self.url)),
auth=(self.xbmc_username, self.xbmc_password),
timeout=(5, 5))
# We don't need the result
except Exception as e:
log.error('Image_Cache_Thread exception: %s' % e)
import traceback
log.error("Traceback:\n%s" % traceback.format_exc())
class Artwork():
lock = Lock()
imageCacheLimitThreads = int(settings('imageCacheLimit'))
imageCacheLimitThreads = imageCacheLimitThreads * 5
enableTextureCache = settings('enableTextureCache') == "true"
if enableTextureCache: if enableTextureCache:
xbmc_port, xbmc_username, xbmc_password = setKodiWebServerDetails() queue = Queue.Queue()
download_thread = Image_Cache_Thread(queue)
imageCacheThreads = [] log.info('Artwork initiated with caching textures = True')
def double_urlencode(self, text): def double_urlencode(self, text):
text = self.single_urlencode(text) text = self.single_urlencode(text)
@ -249,27 +307,6 @@ class Artwork():
pdialog.close() pdialog.close()
def addWorkerImageCacheThread(self, url):
while True:
# removed finished
with self.lock:
for thread in self.imageCacheThreads:
if thread.is_finished:
self.imageCacheThreads.remove(thread)
# add a new thread or wait and retry if we hit our limit
with self.lock:
if len(self.imageCacheThreads) < self.imageCacheLimitThreads:
newThread = image_cache_thread.ImageCacheThread()
newThread.set_url(self.double_urlencode(url))
newThread.set_host(self.xbmc_host, self.xbmc_port)
newThread.set_auth(self.xbmc_username, self.xbmc_password)
newThread.start()
self.imageCacheThreads.append(newThread)
return
log.debug("Waiting for empty queue spot: %s"
% len(self.imageCacheThreads))
xbmc.sleep(50)
def cacheTexture(self, url): def cacheTexture(self, url):
# Cache a single image url to the texture cache # Cache a single image url to the texture cache
if url and self.enableTextureCache: if url and self.enableTextureCache:
@ -289,7 +326,7 @@ class Artwork():
except: except:
pass pass
else: else:
self.addWorkerImageCacheThread(url) self.queue.put(url)
def addArtwork(self, artwork, kodiId, mediaType, cursor): def addArtwork(self, artwork, kodiId, mediaType, cursor):
# Kodi conversion table # Kodi conversion table