[PATCH 08/13] Change keepalive() to spawn IdleThreads

Ethan Glasser-Camp ethan at betacantrips.com
Sun Feb 6 16:59:02 GMT 2011


This is the commit that enables IDLE support. In order to do this, we
hijack the keepalive method. Instead of just sending NOOPs, it now
sends IDLE and responds accordingly, thanks to the IdleThread class.

This code was originally by James Bunton <jamesbunton at fastmail.fm>.
---
 offlineimap/imapserver.py |   30 +++++++++++++-----------------
 1 files changed, 13 insertions(+), 17 deletions(-)

diff --git a/offlineimap/imapserver.py b/offlineimap/imapserver.py
index cbb7f04..b501a0d 100644
--- a/offlineimap/imapserver.py
+++ b/offlineimap/imapserver.py
@@ -363,8 +363,6 @@ class IMAPServer:
         self.ui.debug('imap', 'keepalive thread started')
         while 1:
             self.ui.debug('imap', 'keepalive: top of loop')
-            time.sleep(timeout)
-            self.ui.debug('imap', 'keepalive: after wait')
             if event.isSet():
                 self.ui.debug('imap', 'keepalive: event is set; exiting')
                 return
@@ -375,29 +373,27 @@ class IMAPServer:
             self.connectionlock.release()
             self.ui.debug('imap', 'keepalive: connectionlock released')
             threads = []
-            imapobjs = []
         
             for i in range(numconnections):
                 self.ui.debug('imap', 'keepalive: processing connection %d of %d' % (i, numconnections))
-                imapobj = self.acquireconnection()
-                self.ui.debug('imap', 'keepalive: connection %d acquired' % i)
-                imapobjs.append(imapobj)
-                thr = threadutil.ExitNotifyThread(target = imapobj.noop)
-                thr.setDaemon(1)
-                thr.start()
-                threads.append(thr)
+                if len(self.idlefolders) > i:
+                    idler = IdleThread(self, self.idlefolders[i])
+                else:
+                    idler = IdleThread(self)
+                idler.start()
+                threads.append(idler)
                 self.ui.debug('imap', 'keepalive: thread started')
 
+            self.ui.debug('imap', 'keepalive: waiting for timeout')
+            event.wait(timeout)
+            self.ui.debug('imap', 'keepalive: after wait')
+
             self.ui.debug('imap', 'keepalive: joining threads')
 
-            for thr in threads:
+            for idler in threads:
                 # Make sure all the commands have completed.
-                thr.join()
-
-            self.ui.debug('imap', 'keepalive: releasing connections')
-
-            for imapobj in imapobjs:
-                self.releaseconnection(imapobj)
+                idler.stop()
+                idler.join()
 
             self.ui.debug('imap', 'keepalive: bottom of loop')
 
-- 
1.7.1





More information about the OfflineIMAP-project mailing list