与Python中的多处理相比,并发.未来有什么优势? [英] What are the advantages of concurrent.futures over multiprocessing in Python?

查看:82
本文介绍了与Python中的多处理相比,并发.未来有什么优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Python编写应用程序,我需要同时运行一些任务.该模块的多处理提供了Process类,而current.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?

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

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

推荐答案

以我的实践经验,current.futures为长期运行的任务提交和监视情况提供了更方便的编程模型.我最近使用current.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中的多处理相比,并发.未来有什么优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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