[PATCH 3/3] threading: add comments around semaphore

Nicolas Sebrecht nicolas.s-dev at laposte.net
Wed May 18 03:13:03 BST 2016


Signed-off-by: Nicolas Sebrecht <nicolas.s-dev at laposte.net>
---
 offlineimap/folder/Base.py | 4 ++--
 offlineimap/imapserver.py  | 4 +++-
 offlineimap/threadutil.py  | 3 +++
 3 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/offlineimap/folder/Base.py b/offlineimap/folder/Base.py
index b71396a..d5e9b92 100644
--- a/offlineimap/folder/Base.py
+++ b/offlineimap/folder/Base.py
@@ -876,7 +876,7 @@ class BaseFolder(object):
                 thread = threadutil.InstanceLimitedThread(
                     self.getinstancelimitnamespace(),
                     target = self.copymessageto,
-                    name = "Copy message from %s:%s" % (self.repository, self),
+                    name = "Copy message from %s:%s"% (self.repository, self),
                     args = (uid, dstfolder, statusfolder)
                     )
                 thread.start()
@@ -884,7 +884,7 @@ class BaseFolder(object):
             else:
                 self.copymessageto(uid, dstfolder, statusfolder, register=0)
         for thread in threads:
-            thread.join()
+            thread.join() # Block until all "copy" threads are done.
 
         # Execute new mail hook if we have new mail.
         if self.have_newmail:
diff --git a/offlineimap/imapserver.py b/offlineimap/imapserver.py
index 14aa753..6256720 100644
--- a/offlineimap/imapserver.py
+++ b/offlineimap/imapserver.py
@@ -116,6 +116,8 @@ class IMAPServer:
         self.availableconnections = []
         self.assignedconnections = []
         self.lastowner = {}
+        # XXX: We already apply the maxconnections option to the number of
+        # max threads in init. Why would we need this at all?
         self.semaphore = BoundedSemaphore(self.maxconnections)
         self.connectionlock = Lock()
         self.reference = repos.getreference()
@@ -616,7 +618,7 @@ class IMAPServer:
         It's OK if we have maxconnections + 1 or 2 threads, which is what this
         will help us do."""
 
-        self.semaphore.acquire()
+        self.semaphore.acquire() # Blocking until maxconnections has free slots.
         self.semaphore.release()
 
     def close(self):
diff --git a/offlineimap/threadutil.py b/offlineimap/threadutil.py
index b275b2c..728925c 100644
--- a/offlineimap/threadutil.py
+++ b/offlineimap/threadutil.py
@@ -244,6 +244,9 @@ class InstanceLimitedThread(ExitNotifyThread):
     def start(self):
         global limitedNamespaces
 
+        # Will block until the semaphore has free slots.
+        # XXX: Might be better done at init time to avoid spawning inactive
+        # threads. This could help removing the semaphore logic in imapserver.py.
         limitedNamespaces[self.limitNamespace].acquire()
         ExitNotifyThread.start(self)
 
-- 
2.7.4





More information about the OfflineIMAP-project mailing list