From: Menno Smits Date: Mon, 18 Apr 2011 22:34:14 +0000 (+0100) Subject: Changed IDLE support to use select instead of polling X-Git-Tag: 0.8~34^2~11 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=f47f16db9b28593ae11e68d02b2ab5cc1bca1745;p=imapclient.git Changed IDLE support to use select instead of polling Also made the idle live test a bit more robust when run against slower servers. --- diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index 174a14b..c1a25d8 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -5,8 +5,8 @@ import re import response_lexer from operator import itemgetter +import select import socket -import time import warnings import imaplib @@ -111,9 +111,10 @@ class IMAPClient(object): if port is None: port = default_port - self._imap = ImapClass(host, port) self.use_uid = use_uid + self.ssl = ssl self.folder_encode = True + self._imap = ImapClass(host, port) self._idle_tag = None @@ -340,51 +341,37 @@ class IMAPClient(object): By default, this method will block until an IDLE response is received. If timeout is provided, the call will block for at - most this number of seconds (approximately) while waiting for - an IDLE response. + most this number of seconds while waiting for an IDLE response. The return value is a list of received IDLE responses. These will be parsed with values converted to appropriate types. For example: - [(1, 'EXISTS'), - ('OK', 'Still here'), - (1, 'FETCH', ('FLAGS', ('\NotJunk',))), - (0, 'EXPUNGE')] - - An attempt is made to group responses that were sent together - by the server by waiting for a little longer after the first - IDLE response is received. + [('OK', 'Still here'), + (1, 'EXISTS'), + (1, 'FETCH', ('FLAGS', ('\\NotJunk',)))] """ - timeouts = 0 - resps = [] - start_t = time.time() - - def blocking_done(): - # Wait for one timed out read to try group IDLE responses - # that were sent together - return resps and timeouts > 1 - - if timeout is None: - done = blocking_done - else: - def done(): - return blocking_done() or (time.time() - start_t >= timeout) - # make the socket non-blocking so the timeout can be # implemented for this call - self._imap.sock.settimeout(0.1) + if self.ssl: + sock = self._imap.sslobj + else: + sock = self._imap.sock + sock.setblocking(0) try: - while not done(): - try: - line = self._imap._get_line() - except socket.timeout: - timeouts += 1 - else: - resps.append(_parse_idle_response(line)) + resps = [] + rs, _, _ = select.select([sock], [], [], timeout) + if rs: + while True: + try: + line = self._imap._get_line() + except (socket.timeout, socket.error): + break + else: + resps.append(_parse_idle_response(line)) return resps finally: - self._imap.sock.settimeout(None) + sock.setblocking(1) def idle_done(self): """Take the server out of IDLE mode. diff --git a/livetest.py b/livetest.py index b4d4d2a..b001280 100755 --- a/livetest.py +++ b/livetest.py @@ -9,7 +9,6 @@ import imp import os import sys import time -import threading from datetime import datetime from ConfigParser import SafeConfigParser, NoOptionError @@ -465,19 +464,24 @@ def createLiveTestClass(conf, use_uid): client2.append('INBOX', SIMPLE_MESSAGE) # Check for the idle data - responses = self.client.idle_check(timeout=1) + responses = self.client.idle_check(timeout=5) text, more_responses = self.client.idle_done() self.assertIn((1, 'EXISTS'), responses) - self.assertIn('idle', text.lower()) - self.assertIsInstance(more_responses, list) - + self.assertTrue(isinstance(text, str)) + self.assertGreater(len(text), 0) + self.assertTrue(isinstance(more_responses, list)) + + # Check for IDLE data returned by idle_done() self.client.idle() + client2.select_folder('INBOX') client2.append('INBOX', SIMPLE_MESSAGE) - time.sleep(1) + time.sleep(2) # Allow some time for the IDLE response to be sent text, responses = self.client.idle_done() self.assertIn((2, 'EXISTS'), responses) - self.assertIn('idle', text.lower()) + self.assertTrue(isinstance(text, str)) + self.assertGreater(len(text), 0) + return LiveTest