Python 3.2引入了Concurrent Futures,它似乎是旧版线程和多处理模块的一些高级组合.
与旧的多处理模块相比,在CPU绑定任务中使用它有什么优缺点?
这篇文章表明他们更容易合作 - 是这样吗?
我是用新的闪亮试验concurrent.futures在Python 3.2中引入模块,和我注意到,几乎相同的代码,使用泳池从concurrent.futures的方式比使用慢multiprocessing.Pool.
这是使用多处理的版本:
def hard_work(n):
# Real hard work here
pass
if __name__ == '__main__':
from multiprocessing import Pool, cpu_count
try:
workers = cpu_count()
except NotImplementedError:
workers = 1
pool = Pool(processes=workers)
result = pool.map(hard_work, range(100, 1000000))
Run Code Online (Sandbox Code Playgroud)
这是使用concurrent.futures:
def hard_work(n):
# Real hard work here
pass
if __name__ == '__main__':
from concurrent.futures import ProcessPoolExecutor, wait
from multiprocessing import cpu_count
try:
workers = cpu_count()
except NotImplementedError:
workers = 1
pool = ProcessPoolExecutor(max_workers=workers)
result = pool.map(hard_work, …Run Code Online (Sandbox Code Playgroud) python concurrency future multiprocessing concurrent.futures
我正在用Python编写应用程序,我需要同时运行一些任务.模块多处理提供类Process,concurrent.futures模块提供ProcessPoolExecutor类.两者似乎都使用多个进程来执行他们的任务,但他们的API却不同.我为什么要使用一个而不是另一个?
我知道在Python 3中添加了concurrent.futures,所以我猜它会更好?
请解释一下这两个课程有什么区别?
我注意到multiprocessingPython 2中存在模块但功能上呢?
python parallel-processing concurrency multiprocessing python-3.x
我有N独立的任务,它们以一定multiprocessing.Pool大小os.cpu_count()(在我的例子中为 8)执行maxtasksperchild=1(即为每个新任务创建一个新的工作进程)。
主脚本可以简化为:
import subprocess as sp
import multiprocessing as mp
def do_work(task: dict) -> dict:
res = {}
# ... work ...
for i in range(5):
out = sp.run(cmd, stdout=sp.PIPE, stderr=sp.PIPE, check=False, timeout=60)
res[i] = out.stdout.decode('utf-8')
# ... some more work ...
return res
if __name__ == '__main__':
tasks = load_tasks_from_file(...) # list of dicts
logger = mp.get_logger()
results = []
with mp.Pool(processes=os.cpu_count(), maxtasksperchild=1) as pool:
for i, res in enumerate(pool.imap_unordered(do_work, …Run Code Online (Sandbox Code Playgroud)