diff options
author | RaNaN <Mast3rRaNaN@hotmail.de> | 2011-06-18 14:37:00 +0200 |
---|---|---|
committer | RaNaN <Mast3rRaNaN@hotmail.de> | 2011-06-18 14:37:00 +0200 |
commit | 40fc4979e4ca1f2a74972f68a88212b16cc26458 (patch) | |
tree | 9d1900929441be5d88f2c8a4602c5249d64f9a51 | |
parent | fixes setup unicode errors (diff) | |
download | pyload-40fc4979e4ca1f2a74972f68a88212b16cc26458.tar.xz |
some more locks
-rw-r--r-- | module/PluginThread.py | 2 | ||||
-rw-r--r-- | module/PyFile.py | 20 | ||||
-rw-r--r-- | module/ThreadManager.py | 4 | ||||
-rw-r--r-- | module/Utils.py | 8 | ||||
-rw-r--r-- | module/database/FileDatabase.py | 14 |
5 files changed, 27 insertions, 21 deletions
diff --git a/module/PluginThread.py b/module/PluginThread.py index 1fe1363bc..5f311aa07 100644 --- a/module/PluginThread.py +++ b/module/PluginThread.py @@ -143,7 +143,7 @@ class DownloadThread(PluginThread): try: - if not hasattr(pyfile, "plugin"): continue + if not pyfile.hasPlugin(): continue #this pyfile was deleted while queueing pyfile.plugin.checkForSameFiles(starting=True) diff --git a/module/PyFile.py b/module/PyFile.py index 106d601cc..776039ef8 100644 --- a/module/PyFile.py +++ b/module/PyFile.py @@ -19,10 +19,12 @@ from module.PullEvents import UpdateEvent from module.Progress import Progress -from module.utils import formatSize +from module.utils import formatSize, lock from time import sleep, time +from threading import RLock + statusMap = { "finished": 0, "offline": 1, @@ -59,6 +61,8 @@ class PyFile(object): self.error = error self.order = order # database information ends here + + self.lock = RLock() self.plugin = None #self.download = None @@ -85,13 +89,21 @@ class PyFile(object): def __repr__(self): return "PyFile %s: %s@%s" % (self.id, self.name, self.pluginname) + @lock def initPlugin(self): """ inits plugin instance """ if not self.plugin: self.pluginmodule = self.m.core.pluginManager.getPlugin(self.pluginname) self.pluginclass = getattr(self.pluginmodule, self.m.core.pluginManager.getPluginName(self.pluginname)) self.plugin = self.pluginclass(self) - + + @lock + def hasPlugin(self): + """Thread safe way to determine this file has initialized plugin attribute + + :return: + """ + return hasattr(self, "plugin") and self.plugin def package(self): """ return package instance""" @@ -110,6 +122,7 @@ class PyFile(object): """sync PyFile instance with database""" self.m.updateLink(self) + @lock def release(self): """sync and remove from cache""" self.sync() @@ -157,7 +170,8 @@ class PyFile(object): 'progress': self.progress.getPercent(), } } - + + @lock def abortDownload(self): """abort pyfile if possible""" while self.id in self.m.core.threadManager.processingIds(): diff --git a/module/ThreadManager.py b/module/ThreadManager.py index deefe125e..adf17ee8c 100644 --- a/module/ThreadManager.py +++ b/module/ThreadManager.py @@ -216,11 +216,11 @@ class ThreadManager: free = [x for x in self.threads if not x.active] - inuse = set([(x.active.pluginname,self.getLimit(x)) for x in self.threads if x.active and hasattr(x.active, "plugin") and x.active.plugin.account]) + inuse = set([(x.active.pluginname,self.getLimit(x)) for x in self.threads if x.active and x.active.hasPlugin() and x.active.plugin.account]) inuse = map(lambda x : (x[0], x[1], len([y for y in self.threads if y.active and y.active.pluginname == x[0]])) ,inuse) onlimit = [x[0] for x in inuse if x[1] > 0 and x[2] >= x[1]] - occ = [x.active.pluginname for x in self.threads if x.active and not x.active.plugin.multiDL] + onlimit + occ = [x.active.pluginname for x in self.threads if x.active and x.active.hasPlugin() and not x.active.plugin.multiDL] + onlimit occ.sort() occ = tuple(set(occ)) diff --git a/module/Utils.py b/module/Utils.py index 45fa81d52..0ba3fb035 100644 --- a/module/Utils.py +++ b/module/Utils.py @@ -144,10 +144,12 @@ def parseFileSize(string): #returns bytes def lock(func): def new(*args): + #print "Handler: %s args: %s" % (func,args[1:]) args[0].lock.acquire() - res = func(*args) - args[0].lock.release() - return res + try: + return func(*args) + finally: + args[0].lock.release() return new diff --git a/module/database/FileDatabase.py b/module/database/FileDatabase.py index 6ea7d1104..0ed526240 100644 --- a/module/database/FileDatabase.py +++ b/module/database/FileDatabase.py @@ -21,7 +21,7 @@ from threading import RLock from time import time -from module.utils import formatSize +from module.utils import formatSize, lock from module.PullEvents import InsertEvent, ReloadAllEvent, RemoveEvent, UpdateEvent from module.PyPackage import PyPackage from module.PyFile import PyFile @@ -67,17 +67,7 @@ class FileHandler: args[0].jobCache = {} return func(*args) return new - - def lock(func): - def new(*args): - #print "Handler: %s args: %s" % (func,args[1:]) - args[0].lock.acquire() - res = func(*args) - args[0].lock.release() - #print "Handler: %s return: %s" % (func, res) - return res - return new - + #---------------------------------------------------------------------- def save(self): """saves all data to backend""" |