Gmail API messages.modify 40x slower than IMAP?
<p>I'm creating an app to move messages around in a user's inbox. Currently I'm using the Gmail API to do so, but I've noticed that making requests to the API is markedly slower than using IMAP. </p>

<p>The method is straightforward: I'm sending a batch of modify requests to change the labels on a group of emails in order to move them around. My message throughput using the Gmail API is 3.3 messages/second. For comparison, IMAP throughput is 130 messages/second.</p>

<p>Here's how I'm currently using BatchHttpRequests:</p>

<pre><code>batch = BatchHttpRequest()
for gmailId in gmailIds:
    batch.add(self.service.users().messages().modify(userId=self.user.email, id=gmailId, body=labels))
try:
    batch.execute()
except errors.HttpError, error:
    log_this_error(self.user.email, error.resp.status)
</code></pre>

<p>and here is my IMAP code that accomplishes the same task:</p>

<pre><code># imap_conn is the user's authed connection
status, count = imap_conn.select(oldMailboxLabel)
if status == "OK":
    count = int(count[0])
    if count &gt; 0:
        messageNumbers = range(1, count + 1)
        messageNumbers = ",".join(map(str, messageNumbers))
        imap_conn.copy(messageNumbers, newMailboxLabel)
        imap_conn.close()
        imap_conn.delete(oldMailboxLabel)
    else:
        imap_conn.close()
else:
    count = 0
</code></pre>

<p>I'm already batching and gzipping my requests, and I've tried using fields to limit the amount of information that is returned. All of which is suggested on their <a href="https://developers.google.com/gmail/api/guides/performance" rel="nofollow">performance tips</a> page. I'm struggling to understand why the requests take so long to complete. Any suggestions would be greatly appreciated!</p>

<p>Reading through the GMail API you seem to be doing it "right". I'm going to guess that because GMail is itself a mail client the GMail API is targeted at writing toy mail clients while IMAP is better designed for the efficient bulk work of a fully functional mail client.</p>

<p>If you're moving <em>every</em> message from an old label to a new label, perhaps instead <a href="https://developers.google.com/gmail/api/v1/reference/users/labels/update" rel="nofollow">rename the label using Users.labels.update</a>?</p>

<p>If not, you can reduce the number of requests by doing by <a href="https://developers.google.com/gmail/api/v1/reference/users/threads/modify" rel="nofollow">by thread</a> instead of by message.</p>

<p>Or just use IMAP.</p>

python imap gmail-api