Search code examples
pythontimeoutimappython-idleimaplib

How to tell whether imaplib2 idle response resulted from timeout


I'm using imaplib2 (docs) to interact with an IMAP server.

I'm using the idle command, with a timeout and a callback.

The problem is, I don't see any way of telling if the callback was triggered by the timeout being reached, or if there was a change on the server that I need to check out.

I just get ('OK', ['IDLE terminated (Success)']) every time.

Here's the debug output for both cases:

Timedout:

15:43.94 MainThread server IDLE started, timeout in 5.00 secs
15:48.94 imap.gmail.com handler server IDLE timedout
15:48.94 imap.gmail.com handler server IDLE finished
15:48.94 imap.gmail.com writer > DONE\r\n
15:49.17 imap.gmail.com reader < DDDM6 OK IDLE terminated (Success)\r\n
15:49.17 imap.gmail.com handler _request_pop(DDDM6, ('OK', ['IDLE terminated (Success)']))

Something happened:

18:41.34 MainThread server IDLE started, timeout in 50.00 secs
19:01.35 imap.gmail.com reader < * 1 EXISTS\r\n
19:01.37 imap.gmail.com handler server IDLE finished
19:01.37 imap.gmail.com writer > DONE\r\n
19:01.59 imap.gmail.com reader < BFCN6 OK IDLE terminated (Success)\r\n
19:01.59 imap.gmail.com handler _request_pop(BFCN6, ('OK', ['IDLE terminated (Success)']))

What am I missing?

Does the functionality just not exist in imaplib2?


Solution

  • Piers Lauder (author of imaplib2) just answered this question on the imaplib2-devel mailing list. He said:

    I think the way to test if an IDLE has timed out is to execute:

    instance.response('IDLE')
    

    which will return:

    ('IDLE', ['TIMEOUT'])
    

    if the reason that the idle returned as a timeout, rather than something else (such as ('IDLE', [None])).

    I agree that this should be documented, so I'll fix the imaplib2.html document