From 83539f86013f66d2172687366d3dcbc86461f005 Mon Sep 17 00:00:00 2001 From: Nicolas Sebrecht Date: Tue, 17 May 2016 03:53:09 +0200 Subject: [PATCH] threading: minor improvements Factorize string, enhance comments and minor code improvements. Signed-off-by: Nicolas Sebrecht --- offlineimap/accounts.py | 5 +++-- offlineimap/folder/Base.py | 5 +++-- offlineimap/init.py | 22 ++++++++++++++++------ offlineimap/threadutil.py | 7 +++++-- 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/offlineimap/accounts.py b/offlineimap/accounts.py index 5acd82a..3cf4769 100644 --- a/offlineimap/accounts.py +++ b/offlineimap/accounts.py @@ -351,11 +351,12 @@ class SyncableAccount(Account): "[%s]"% (localfolder, localfolder.repository)) continue # Ignore filtered folder if not globals.options.singlethreading: - thread = InstanceLimitedThread(\ + thread = InstanceLimitedThread( instancename = 'FOLDER_' + self.remoterepos.getname(), target = syncfolder, name = "Folder %s [acc: %s]"% (remotefolder.getexplainedname(), self), - args = (self, remotefolder, quick)) + args = (self, remotefolder, quick) + ) thread.start() folderthreads.append(thread) else: diff --git a/offlineimap/folder/Base.py b/offlineimap/folder/Base.py index 1bed3bf..a41566d 100644 --- a/offlineimap/folder/Base.py +++ b/offlineimap/folder/Base.py @@ -871,11 +871,12 @@ class BaseFolder(object): # exceptions are caught in copymessageto() if self.suggeststhreads() and not globals.options.singlethreading: self.waitforthread() - thread = threadutil.InstanceLimitedThread(\ + thread = threadutil.InstanceLimitedThread( self.getcopyinstancelimit(), target = self.copymessageto, name = "Copy message from %s:%s" % (self.repository, self), - args = (uid, dstfolder, statusfolder)) + args = (uid, dstfolder, statusfolder) + ) thread.start() threads.append(thread) else: diff --git a/offlineimap/init.py b/offlineimap/init.py index b04c9db..81d0c33 100644 --- a/offlineimap/init.py +++ b/offlineimap/init.py @@ -35,6 +35,7 @@ from offlineimap.repository import Repository import traceback import collections +ACCOUNT_LIMITED_THREAD_NAME = 'MAX_ACCOUNTS' def syncitall(list_accounts, config): """The target when in multithreading mode for running accounts threads.""" @@ -44,7 +45,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 = 'ACCOUNTLIMIT', + instancename = ACCOUNT_LIMITED_THREAD_NAME, target = account.syncrunner, name = "Account sync %s"% accountname ) @@ -287,18 +288,27 @@ class OfflineImap: if socktimeout > 0: socket.setdefaulttimeout(socktimeout) - threadutil.initInstanceLimit('ACCOUNTLIMIT', - config.getdefaultint('general', 'maxsyncaccounts', 1)) + threadutil.initInstanceLimit( + ACCOUNT_LIMITED_THREAD_NAME, + config.getdefaultint('general', 'maxsyncaccounts', 1) + ) for reposname in config.getsectionlist('Repository'): + # XXX: We are likely lying around. If we must use at most n + # 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]: if options.singlethreading: threadutil.initInstanceLimit(instancename, 1) else: - threadutil.initInstanceLimit(instancename, - config.getdefaultint('Repository ' + reposname, - 'maxconnections', 2)) + threadutil.initInstanceLimit( + instancename, + config.getdefaultint( + 'Repository ' + reposname, + 'maxconnections', 2) + ) self.config = config return (options, args) diff --git a/offlineimap/threadutil.py b/offlineimap/threadutil.py index 894fd15..1802053 100644 --- a/offlineimap/threadutil.py +++ b/offlineimap/threadutil.py @@ -224,8 +224,10 @@ class ExitNotifyThread(Thread): instancelimitedsems = {} def initInstanceLimit(instancename, instancemax): - """Initialize the instance-limited thread implementation to permit - up to intancemax threads with the given instancename.""" + """Initialize the instance-limited thread implementation. + + Run up to intancemax threads for the given instancename. This allows + to honor maxsyncaccounts and maxconnections.""" global instancelimitedsems @@ -235,6 +237,7 @@ def initInstanceLimit(instancename, instancemax): class InstanceLimitedThread(ExitNotifyThread): def __init__(self, instancename, *args, **kwargs): + # XXX: this is not a instance name, is it? self.instancename = instancename super(InstanceLimitedThread, self).__init__(*args, **kwargs)