与 Python 中的多处理相比,concurrent.futures 有哪些优势?

问题描述

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

I'm writing an app in Python and I need to run some tasks simultaneously. The module multiprocessing offers the class Process and the concurrent.futures module has the class ProcessPoolExecutor. Both seem to use multiple processes to execute their tasks, but their APIs are different. Why should I use one over the other?

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

I know that concurrent.futures was added in Python 3, so I guess it's better?


解决方案

concurrent.futures 的动机在 PEP.

The motivations for concurrent.futures are covered in the PEP.

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

In my practical experience concurrent.futures provides a more convenient programming model for long-running task submission and monitoring situations. A program I recently wrote using concurrent.futures involved monitoring a directory for incoming files over a 2-3 hour window, translating each file as it arrives to a task, submitting it and so on. Future objects returned by the ProcessPoolExecutor allow for tracking task status, providing intermediate status reports etc in a convenient way.

相关文章