与 Python 中的多处理相比,concurrent.futures 有哪些优势? [英] What are the advantages of concurrent.futures over multiprocessing in Python?

查看:22
本文介绍了与 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.

这篇关于与 Python 中的多处理相比,concurrent.futures 有哪些优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆