編寫一個程序,驗證電子郵件語法和MX記錄的列表,阻止編程費時,我想這樣做異步或線程,這是我的代碼:扭曲執行的同時,10個線程,並等待結果
with open(file_path) as f:
# check the status of file, if away then file pointer will be at the last index
if (importState.status == ImportStateFile.STATUS_AWAY):
f.seek(importState.fileIndex, 0)
while True:
# the number of emails to process is configurable 10 or 20
emails = list(islice(f, app.config['NUMBER_EMAILS_TO_PROCESS']))
if len(emails) == 0:
break;
importState.fileIndex = importState.fileIndex + len(''.join(emails))
for email in emails:
email = email.strip('''<>;,'\r\n ''').lower()
d = threads.deferToThread(check_email, email)
d.addCallback(save_email_status, email, importState)
# set the number of emails processed
yield set_nbrs_emails_process(importState)
# do an insert of all emails
yield reactor.callFromThread(db.session.commit)
# set file status as success
yield finalize_import_file_state(importState)
reactor.callFromThread(reactor.stop)
檢查電子郵件功能:
def check_email(email):
pipe = subprocess.Popen(["./check_email", '--email=%s' % email], stdout=subprocess.PIPE)
status = pipe.stdout.read()
try:
status = int(status)
except ValueError:
status = -1
return status
什麼,我需要的是過程,同時10封電子郵件,並等待結果。
有10封電子郵件,還是您想同時發送不超過10封電子郵件? – jfs 2014-10-10 00:00:22
你的代碼中是否有'@ inlineCallBacks'裝飾器(隱含所有'yield'語句)? – jfs 2014-10-10 00:02:28
是的,有@ @ inlineCallBacks',我想通過批量處理10或20封電子郵件,然後將它插入到數據庫中。 – 2014-10-10 07:24:55