Revert "Dedicated art downloading queue"

This reverts commit dc94cc6bfe.
This commit is contained in:
tomkat83 2016-09-17 10:58:08 +02:00
parent dc94cc6bfe
commit 6621cc5a8d

View file

@ -8,8 +8,7 @@ import requests
import os import os
import urllib import urllib
from sqlite3 import OperationalError from sqlite3 import OperationalError
from threading import Lock, Thread from threading import Lock
import Queue
import xbmc import xbmc
import xbmcgui import xbmcgui
@ -17,8 +16,7 @@ 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, ThreadMethods, ThreadMethodsAdditionalSuspend, \ tryDecode, IfExists
ThreadMethodsAdditionalStop
# Disable annoying requests warnings # Disable annoying requests warnings
import requests.packages.urllib3 import requests.packages.urllib3
@ -121,78 +119,22 @@ def setKodiWebServerDetails():
return (xbmc_port, xbmc_username, xbmc_password) return (xbmc_port, xbmc_username, xbmc_password)
@ThreadMethodsAdditionalSuspend('suspend_LibraryThread') class Artwork():
@ThreadMethodsAdditionalStop('plex_shouldStop') lock = Lock()
@ThreadMethods
class Image_Cache_Thread(Thread): enableTextureCache = settings('enableTextureCache') == "true"
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 = []
def __init__(self, queue): xbmc_host = 'localhost'
self.queue = queue xbmc_port = None
Thread.__init__(self) xbmc_username = None
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:
queue = Queue.Queue() xbmc_port, xbmc_username, xbmc_password = setKodiWebServerDetails()
download_thread = Image_Cache_Thread(queue)
log.info('Artwork initiated with caching textures = True') imageCacheThreads = []
def double_urlencode(self, text): def double_urlencode(self, text):
text = self.single_urlencode(text) text = self.single_urlencode(text)
@ -307,6 +249,27 @@ 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:
@ -326,7 +289,7 @@ class Artwork():
except: except:
pass pass
else: else:
self.queue.put(url) self.addWorkerImageCacheThread(url)
def addArtwork(self, artwork, kodiId, mediaType, cursor): def addArtwork(self, artwork, kodiId, mediaType, cursor):
# Kodi conversion table # Kodi conversion table