title: Multi threading IMAP backup [python]
body: <p>I have the following python script which works freat to save IMAP folder to local .eml files. I want to do backup of my old email, but it has pretty large inbox. Is there anyway the below code can be modified with multi threads? Or perhaps several loops, so that it works faster on large IMAP folders?</p>
  
  <p>Thanks a mil!</p>
  
  <pre><code>import sys
  import imaplib
  import getpass
  
  IMAP_SERVER = ''
  EMAIL_ACCOUNT = ""
  EMAIL_FOLDER = "Inbox"
  OUTPUT_DIRECTORY = './'
  
  PASSWORD = getpass.getpass()
  
  
  def process_mailbox(M):
      """
      Dump all emails in the folder to files in output directory.
      """
  
      rv, data = M.search(None, "ALL")
      if rv != 'OK':
          print "No messages found!"
          return
  
      for num in data[0].split():
          rv, data = M.fetch(num, '(RFC822)')
          if rv != 'OK':
              print "ERROR getting message", num
              return
          print "Writing message ", num
  
          f = open('%s/%s.eml' %(OUTPUT_DIRECTORY, num), 'wb')
          f.write(data[0][1])
          f.close()
  
  def main():
      M = imaplib.IMAP4_SSL(IMAP_SERVER)
      M.login(EMAIL_ACCOUNT, PASSWORD)
      rv, data = M.select(EMAIL_FOLDER)
      if rv == 'OK':
          print "Processing mailbox: ", EMAIL_FOLDER
          process_mailbox(M)
          M.close()
      else:
          print "ERROR: Unable to open mailbox ", rv
      M.logout()
  
  if __name__ == "__main__":
      main()
  </code></pre>