From 65c82894b225a2a098c39fafd9ad4101a3f13720 Mon Sep 17 00:00:00 2001 From: DiMartinoXBMC Date: Wed, 5 Aug 2015 00:40:25 +0300 Subject: [PATCH] threading update by srg70 --- BTClientPlayer.py | 59 +++++++++++++++++++++++++++-------------------- SearcherABC.py | 5 ---- default.py | 6 ++++- functions.py | 57 +++++++++++++++++++++++++++++---------------- 4 files changed, 76 insertions(+), 51 deletions(-) diff --git a/BTClientPlayer.py b/BTClientPlayer.py index e8d15bd..0c2d414 100644 --- a/BTClientPlayer.py +++ b/BTClientPlayer.py @@ -223,34 +223,43 @@ class BTClientPlayer(xbmc.Player): # self.torrent.continueSession(ind) def buffer(self): - #iterator = 0 + iterator = 0 progressBar = xbmcgui.DialogProgress() progressBar.create(self.localize('Please Wait') + str(' [%s]' % str(self.lt.version)), self.localize('Seeds searching.')) - while not self.c.is_file_ready: #iterator < 100:#or not self.torrent.is_playble() + while iterator < 100 or not self.c.is_file_ready:# not self.c.is_file_ready + iterator = 0 + ready_list=[] status = self.c.get_normalized_status() - iterator = int(status['progress'] * 10000) - if iterator > 99: iterator = 99 + conditions=[status['state'] in ['downloading', 'finished', 'seeding'], status['desired_rate'] > 0, + status['progress'] > 0, + status['desired_rate'] > 0 and (status['download_rate'] > status['desired_rate'] or + status['download_rate'] * status['progress'] * 100 > status['desired_rate'])] + for cond in conditions: + if cond: + ready_list.append(True) + iterator+=100/len(conditions) + else: + ready_list.append(False) + + speedsText = '%s: %s Mbit/s %s %s: %s Mbit/s' % (self.localize('Bitrate'), str(int(status['desired_rate'] * 8 / (1024 * 1024))) if status['desired_rate'] else 0, + '[COLOR=green]>[/COLOR]' if ready_list[3] else '[COLOR=red]<[/COLOR]', + self.localize('Download speed'),str(status['download_rate'] * 8 / 1000000)) + if status['state'] in ['queued','checking','checking fastresume'] or (status['progress'] == 0 and status['num_pieces'] > 0): - progressBar.update(iterator, self.localize('Checking preloaded files...'), ' ', ' ') - elif status['state'] == 'downloading': - dialogText = self.localize('Preloaded: ') + str(status['downloaded'] / 1024 / 1024) + ' MB / ' + str( - status['total_size'] / 1024 / 1024) + ' MB' - peersText = ' [%s: %s; %s: %s]' % ( - self.localize('Seeds'), str(status['seeds_connected']), self.localize('Peers'), + progressBar.update(iterator, self.localize('Checking preloaded files...'), speedsText, ' ') + + elif status['state'] in ['downloading', 'finished', 'seeding']: + dialogText = self.localize('Preloaded: ') + '%s MB / %s MB' % \ + (str(status['downloaded'] / 1024 / 1024), str(status['total_size'] / 1024 / 1024)) + peersText = '[%s: %s; %s: %s]' % (self.localize('Seeds'), str(status['seeds_connected']), self.localize('Peers'), str(status['peers_connected']),) - speedsText = '%s: %s Mbit/s; %s: %s Mbit/s' % ( - self.localize('Downloading'), str(status['download_rate'] * 8 / 1000000), - self.localize('Uploading'), str(status['upload_rate'] * 8 / 1000000)) - #if self.debug: - # peersText=peersText + ' ' + self.torrent.get_debug_info('dht_state') - # dialogText=dialogText.replace(self.localize('Preloaded: '),'') + ' ' + self.torrent.get_debug_info('trackers_sum') - progressBar.update(iterator, self.localize('Seeds searching.') + peersText, dialogText, - speedsText) + progressBar.update(iterator, peersText, speedsText, dialogText, + ) else: progressBar.update(iterator, self.localize('UNKNOWN STATUS'), ' ', ' ') if progressBar.iscanceled(): - self.c.close() + self.on_exit() break xbmc.sleep(1000) progressBar.update(0) @@ -291,6 +300,7 @@ class BTClientPlayer(xbmc.Player): self.basename = label #self.seeding_run = False listitem = xbmcgui.ListItem(label) + info={} #if self.subs_dl: # self.setup_subs(label, path) @@ -306,16 +316,15 @@ class BTClientPlayer(xbmc.Player): title, int(seasonId), int(self.episodeId), self.basename.split('.')[-1], self.basename) if seasonId and self.episodeId and label and title: - listitem = xbmcgui.ListItem(label) - - listitem.setInfo(type='video', infoLabels={'title': label, - 'episode': int(self.episodeId), - 'season': int(seasonId), - 'tvshowtitle': title}) + info={'title': label, + 'episode': int(self.episodeId), + 'season': int(seasonId), + 'tvshowtitle': title} except: log('[BTClientPlayer] Operation INFO failed!') thumbnail = self.get("thumbnail") + listitem.setInfo(type='Video', infoLabels=info) if thumbnail: listitem.setThumbnailImage(urllib.unquote_plus(thumbnail)) self.display_name = label diff --git a/SearcherABC.py b/SearcherABC.py index baf463b..2f3dee4 100644 --- a/SearcherABC.py +++ b/SearcherABC.py @@ -18,7 +18,6 @@ along with this program. If not, see . ''' -import abc import urllib import urllib2 import cookielib @@ -38,8 +37,6 @@ from functions import log, debug class SearcherABC: - __metaclass__ = abc.ABCMeta - searchIcon = '/icons/video.png' sourceWeight = 1 cookieJar = None @@ -56,7 +53,6 @@ class SearcherABC: def __init__(self): print '!!!!!!!!!!!!!!!!!! BORN '+self.__class__.__name__ - @abc.abstractmethod def search(self, keyword): ''' Retrieve keyword from the input and return a list of tuples: @@ -70,7 +66,6 @@ class SearcherABC: ''' return - @abc.abstractproperty def isMagnetLinkSource(self): return 'Should never see this' diff --git a/default.py b/default.py index ed8a3dc..61f0b6b 100644 --- a/default.py +++ b/default.py @@ -19,7 +19,7 @@ ''' import sys - +import gc import xbmcaddon @@ -38,3 +38,7 @@ if (__name__ == "__main__" ): else: params = core.getParameters(sys.argv[2]) core.executeAction(params) + del core + +collected = gc.collect() +print "Garbage collector: collected %d objects." % (collected) \ No newline at end of file diff --git a/functions.py b/functions.py index 49bdd44..bc1a35b 100644 --- a/functions.py +++ b/functions.py @@ -1066,13 +1066,14 @@ class Searchers(): def searchWithSearcher(self, keyword, searcher): import traceback filesList = [] - slist = Searchers().list() + slist = self.list() if slist[searcher]['path'] not in sys.path: sys.path.insert(0, slist[searcher]['path']) print 'Added %s in sys.path' % (slist[searcher]['path']) try: - searcherObject = getattr(__import__(searcher), searcher)() - filesList = searcherObject.search(keyword) + searcherObject = getattr(__import__(searcher), searcher) + filesList = searcherObject().search(keyword) + del searcherObject except Exception, e: print 'Unable to use searcher: ' + searcher + ' at ' + __plugin__ + ' searchWithSearcher(). Exception: ' + str( e) @@ -1111,9 +1112,9 @@ class Searchers(): def search(url, searchersList, isApi=None): from threading import Thread try: - from Queue import Queue + from Queue import Queue, Empty except ImportError: - from queue import Queue + from queue import Queue, Empty num_threads=__settings__.getSetting('num_threads') if num_threads and int(num_threads)>0: @@ -1124,38 +1125,54 @@ def search(url, searchersList, isApi=None): result = {} iterator, filesList, left_searchers = 0, [], [] timeout_multi=int(sys.modules["__main__"].__settings__.getSetting("timeout")) - wait_time=10+(10*timeout_multi) left_searchers.extend(searchersList) if not isApi: progressBar = xbmcgui.DialogProgress() progressBar.create(Localization.localize('Please Wait'), Localization.localize('Materials are loading now.')) + class CleanExit: + pass + def search_one(i, q): while True: - if not isApi and progressBar.iscanceled(): - progressBar.update(0) - progressBar.close() - return - iterator=100*int(len(searchersList)-len(left_searchers))/len(searchersList) - if not isApi: - progressBar.update(int(iterator), join_list(left_searchers, replace='.py')) - searcherFile = q.get() - searcher=searcherFile.replace('.py','') - print "Thread %s: Searching at %s" % (i, searcher) - result[searcherFile]=Searchers().searchWithSearcher(url, searcher) - left_searchers.remove(searcherFile) - q.task_done() + try: + if not isApi and progressBar.iscanceled(): + progressBar.update(0) + progressBar.close() + return + iterator=100*int(len(searchersList)-len(left_searchers))/len(searchersList) + if not isApi: + progressBar.update(int(iterator), join_list(left_searchers, replace='.py')) + searcherFile = q.get_nowait() + if searcherFile == CleanExit: + sys.exit() + searcher=searcherFile.replace('.py','') + print "Thread %s: Searching at %s" % (i, searcher) + result[searcherFile]=Searchers().searchWithSearcher(url, searcher) + left_searchers.remove(searcherFile) + q.task_done() + except Empty: + pass + workers=[] for i in range(num_threads): worker = Thread(target=search_one, args=(i, queue)) worker.setDaemon(True) worker.start() + workers.append(worker) for searcherFile in searchersList: - queue.put(searcherFile, timeout=wait_time) + queue.put(searcherFile) print "Main Thread Waiting" queue.join() + for i in range(num_threads): + queue.put(CleanExit) + + print "Main Thread Waiting for Threads" + for w in workers: + w.join() + print "Done" if not isApi: