【问题标题】:What are the advantages of concurrent.futures over multiprocessing in Python?与 Python 中的多处理相比,concurrent.futures 有哪些优势?
【发布时间】:2012-07-21 23:44:25
【问题描述】:

我正在用 Python 编写一个应用程序,我需要同时运行一些任务。模块 multiprocessing 提供类 Process 并且 concurrent.futures 模块具有类 ProcessPoolExecutor。两者似乎都使用多个进程来执行它们的任务,但它们的 API 不同。为什么我应该使用一个而不是另一个?

我知道 concurrent.futures 是在 Python 3 中添加的,所以我想它会更好吗?

【问题讨论】:

标签: python multithreading multiprocessing concurrent.futures


【解决方案1】:

concurrent.futures 的动机在 PEP 中进行了介绍。

在我的实践经验中,concurrent.futures 为长时间运行的任务提交和监控情况提供了更方便的编程模型。我最近使用 concurrent.futures 编写的一个程序涉及在 2-3 小时的窗口内监视目录中的传入文件,在每个文件到达任务时将其翻译,提交等等。 ProcessPoolExecutor 返回的未来对象允许以方便的方式跟踪任务状态、提供中间状态报告等。

【讨论】:

    猜你喜欢
    • 2013-09-22
    • 2012-05-17
    • 1970-01-01
    • 2010-11-02
    • 2016-01-04
    • 2011-08-02
    • 2011-03-26
    相关资源
    最近更新 更多