Twisted同时执行10个线程并等待结果

时间:2014-10-06 09:48:01

标签: python multithreading python-2.7 twisted

编写一个验证电子邮件语法和MX记录列表的程序,因为阻塞编程非常耗时,我想做这个异步或线程,这是我的代码:

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封电子邮件并等待结果。

1 个答案:

答案 0 :(得分:2)

我不确定为什么你的示例代码中涉及线程。您不需要线程与Twisted交互电子邮件,也不需要同时执行此操作。

如果你有一个返回Deferred的异步函数,你可以调用它十次,十个不同的工作流将并行进行:

for i in range(10):
    async_check_email_returning_deferred()

如果您想知道所有十个结果何时可用,您可以使用gatherResults:

from twisted.internet.defer import gatherResults
...
email_results = []
for i in range(10):
    email_results.append(async_check_mail_returning_deferred())
all_results = gatherResults(email_results)

all_results是一个Deferred,当Deferreds中的所有email_results被解雇时(或当第一个用Failure触发时,它会触发) )。

相关问题