与Python中的多处理相比,并发.未来有什么优势? [英] What are the advantages of concurrent.futures over multiprocessing in 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?
推荐答案
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屋!