目标是使用 imaplib 删除一堆电子邮件。电子邮件文件夹每月接收大约 300k 条新消息。仅应删除超过 1 个月的邮件。如果执行这个脚本,它会删除旧消息,但是删除需要很多时间,而且简单的迭代看起来并不有效。这需要几个小时。通过尝试通过多处理来提高速度会产生错误。
您有什么建议可以提高删除大量消息的速度?
import sys
import datetime
from imaplib import IMAP4
# get the date a month from the current
monthbefore = (datetime.date.today() - datetime.timedelta(365/12)).strftime("%d-%b-%Y")
m = IMAP4('mail.domain.com')
m.login('user@domain.com', 'password')
# shows how many messages in selected folder
print m.select('Folder')
typ, data = m.select('Folder')
# find old messages
typ, data = m.search(None, '(BEFORE %s)' % (monthbefore))
# delete them
print "Will be removed:\t", data[0].split()[-1],"messages"
for num in data[0].split():
m.store(num, '+FLAGS', '\\Deleted')
sys.stderr.write('\rRemoving message:\t %s' % num)
# now expunge marked for deletion messages, close connection and exit
print "\nGet ready for expunge"
m.expunge()
print "Expunged! Quiting."
m.close()
m.logout()
更新:重写了部分代码,这是一个快 1000 倍的工作变体(我的服务器支持一次存储超过 1000 条消息的命令):
def chunks(l, n):
# yields successive n-sized chunks from l.
for i in xrange(0, len(l), n):
yield l[i:i+n]
mcount = data[0].split()[-1]
print "Will be removed", mcount, "messages"
for i in list(chunks(data[0].split(), 1000)):
m.store(",".join(i), '+FLAGS', '\\Deleted')
sys.stderr.write('\rdone {0:.2f}%'.format((int(i[-1])/int(mcount)*100)))