From e0fdcb385270d496c5d928ea90434ef5620045c9 Mon Sep 17 00:00:00 2001 From: Nicolas Sebrecht Date: Wed, 18 May 2016 02:42:09 +0200 Subject: [PATCH] threading: improve variable names and factorize code Signed-off-by: Nicolas Sebrecht --- offlineimap/accounts.py | 8 ++++++-- offlineimap/folder/Base.py | 4 ++-- offlineimap/folder/IMAP.py | 5 +++-- offlineimap/init.py | 7 ++++--- offlineimap/threadutil.py | 29 ++++++++++++++--------------- 5 files changed, 29 insertions(+), 24 deletions(-) diff --git a/offlineimap/accounts.py b/offlineimap/accounts.py index c9b6dbc..abdbe8c 100644 --- a/offlineimap/accounts.py +++ b/offlineimap/accounts.py @@ -29,6 +29,8 @@ from offlineimap.threadutil import InstanceLimitedThread import six +FOLDER_NAMESPACE = 'LIMITED_FOLDER_' + try: import fcntl except: @@ -354,9 +356,11 @@ class SyncableAccount(Account): continue # Ignore filtered folder if not globals.options.singlethreading: thread = InstanceLimitedThread( - instancename = 'FOLDER_' + self.remoterepos.getname(), + limitNamespace = "%s%s"% ( + FOLDER_NAMESPACE, self.remoterepos.getname()), target = syncfolder, - name = "Folder %s [acc: %s]"% (remotefolder.getexplainedname(), self), + name = "Folder %s [acc: %s]"% ( + remotefolder.getexplainedname(), self), args = (self, remotefolder, quick) ) thread.start() diff --git a/offlineimap/folder/Base.py b/offlineimap/folder/Base.py index a41566d..8b7242b 100644 --- a/offlineimap/folder/Base.py +++ b/offlineimap/folder/Base.py @@ -134,7 +134,7 @@ class BaseFolder(object): return True - def getcopyinstancelimit(self): + def getinstancelimitnamespace(self): """For threading folders, returns the instancelimitname for InstanceLimitedThreads.""" @@ -872,7 +872,7 @@ class BaseFolder(object): if self.suggeststhreads() and not globals.options.singlethreading: self.waitforthread() thread = threadutil.InstanceLimitedThread( - self.getcopyinstancelimit(), + self.getinstancelimitnamespace(), target = self.copymessageto, name = "Copy message from %s:%s" % (self.repository, self), args = (uid, dstfolder, statusfolder) diff --git a/offlineimap/folder/IMAP.py b/offlineimap/folder/IMAP.py index aa59954..bc02b60 100644 --- a/offlineimap/folder/IMAP.py +++ b/offlineimap/folder/IMAP.py @@ -32,6 +32,7 @@ import six # Globals CRLF = '\r\n' +MSGCOPY_NAMESPACE = 'MSGCOPY_' # NB: message returned from getmessage() will have '\n' all over the place, @@ -88,8 +89,8 @@ class IMAPFolder(BaseFolder): OfflineImapError.ERROR.REPO), None, exc_info()[2]) # Interface from BaseFolder - def getcopyinstancelimit(self): - return 'MSGCOPY_' + self.repository.getname() + def getinstancelimitnamespace(self): + return MSGCOPY_NAMESPACE + self.repository.getname() # Interface from BaseFolder def get_uidvalidity(self): diff --git a/offlineimap/init.py b/offlineimap/init.py index 81d0c33..d0a98de 100644 --- a/offlineimap/init.py +++ b/offlineimap/init.py @@ -31,6 +31,7 @@ from offlineimap.ui import UI_LIST, setglobalui, getglobalui from offlineimap.CustomConfig import CustomConfigParser from offlineimap.utils import stacktrace from offlineimap.repository import Repository +from offlineimap.folder.IMAP import MSGCOPY_NAMESPACE import traceback import collections @@ -45,7 +46,7 @@ def syncitall(list_accounts, config): # Start a new thread per account and store it in the collection. account = accounts.SyncableAccount(config, accountname) thread = threadutil.InstanceLimitedThread( - instancename = ACCOUNT_LIMITED_THREAD_NAME, + ACCOUNT_LIMITED_THREAD_NAME, target = account.syncrunner, name = "Account sync %s"% accountname ) @@ -298,8 +299,8 @@ class OfflineImap: # connections for a remote IMAP server, why do we allow twice this # number? The max connections number is used by both the FOLDER_ and # the MSGCOPY_ prefixes! - for instancename in ["FOLDER_" + reposname, - "MSGCOPY_" + reposname]: + for instancename in [accounts.FOLDER_NAMESPACE + reposname, + MSGCOPY_NAMESPACE + reposname]: if options.singlethreading: threadutil.initInstanceLimit(instancename, 1) else: diff --git a/offlineimap/threadutil.py b/offlineimap/threadutil.py index 1802053..2e80595 100644 --- a/offlineimap/threadutil.py +++ b/offlineimap/threadutil.py @@ -221,37 +221,36 @@ class ExitNotifyThread(Thread): # Instance-limited threads ###################################################################### -instancelimitedsems = {} +limitedNamespaces = {} -def initInstanceLimit(instancename, instancemax): +def initInstanceLimit(limitNamespace, instancemax): """Initialize the instance-limited thread implementation. - Run up to intancemax threads for the given instancename. This allows - to honor maxsyncaccounts and maxconnections.""" + Run up to intancemax threads for the given limitNamespace. This allows to + honor maxsyncaccounts and maxconnections.""" - global instancelimitedsems + global limitedNamespaces - if not instancename in instancelimitedsems: - instancelimitedsems[instancename] = BoundedSemaphore(instancemax) + if not limitNamespace in limitedNamespaces: + limitedNamespaces[limitNamespace] = BoundedSemaphore(instancemax) class InstanceLimitedThread(ExitNotifyThread): - def __init__(self, instancename, *args, **kwargs): - # XXX: this is not a instance name, is it? - self.instancename = instancename + def __init__(self, limitNamespace, *args, **kwargs): + self.limitNamespace = limitNamespace super(InstanceLimitedThread, self).__init__(*args, **kwargs) def start(self): - global instancelimitedsems + global limitedNamespaces - instancelimitedsems[self.instancename].acquire() + limitedNamespaces[self.limitNamespace].acquire() ExitNotifyThread.start(self) def run(self): - global instancelimitedsems + global limitedNamespaces try: ExitNotifyThread.run(self) finally: - if instancelimitedsems and instancelimitedsems[self.instancename]: - instancelimitedsems[self.instancename].release() + if limitedNamespaces and limitedNamespaces[self.limitNamespace]: + limitedNamespaces[self.limitNamespace].release()