def ImportMultipleMails(self, user_name, threads_per_batch=20):
    """Launches separate threads to import every message added by AddMailEntry.
    
    Args:
      user_name: The user account name to import messages to.
      threads_per_batch: Number of messages to import at a time.
    
    Returns:
      The number of email messages that were successfully migrated.
    
    Raises:
      Exception: An error occurred while importing mails.
    """
    num_entries = len(self.mail_entries)

    if not num_entries:
      return 0

    threads = []
    for mail_entry_properties in self.mail_entries:
      t = threading.Thread(name=mail_entry_properties.identifier,
                           target=self.ImportMail,
                           args=(user_name, mail_entry_properties.mail_message,
                                 mail_entry_properties.mail_item_properties,
                                 mail_entry_properties.mail_labels))
      threads.append(t)
    try:
      # Determine the number of batches needed with threads_per_batch in each
      batches = num_entries / threads_per_batch + (
          0 if num_entries % threads_per_batch == 0 else 1)
      batch_min = 0
      # Start the threads, one batch at a time
      for batch in range(batches):
        batch_max = ((batch + 1) * threads_per_batch
                     if (batch + 1) * threads_per_batch < num_entries
                     else num_entries)
        for i in range(batch_min, batch_max):
          threads[i].start()
          time.sleep(1)

        for i in range(batch_min, batch_max):
          threads[i].join()

        batch_min = batch_max

      self.mail_entries = []
    except Exception as e:
      raise Exception(e.args[0])
    else:
      return num_entries - self.exceptions
dataset/ETHPy150Open kuri65536/python-for-android/python3-alpha/python-libs/gdata/apps/migration/service.py/MigrationService.ImportMultipleMails/original