“块大小" Python的multiprocessing.Pool.map中的参数 [英] "chunksize" parameter in Python's multiprocessing.Pool.map

查看:90
本文介绍了“块大小" Python的multiprocessing.Pool.map中的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我有一个带有2个处理器的池对象:

p=multiprocessing.Pool(2)

我想遍历目录中的文件列表并使用map函数

有人可以解释一下此函数的chunksize:

p.map(func, iterable[, chunksize])

例如,如果我将块大小设置为10,是否意味着每10个文件应使用一个处理器处理?

解决方案

查看 解决方案

Looking at the documentation for Pool.map it seems you're almost correct: the chunksize parameter will cause the iterable to be split into pieces of approximately that size, and each piece is submitted as a separate task.

So in your example, yes, map will take the first 10 (approximately), submit it as a task for a single processor... then the next 10 will be submitted as another task, and so on. Note that it doesn't mean that this will make the processors alternate every 10 files, it's quite possible that processor #1 ends up getting 1-10 AND 11-20, and processor #2 gets 21-30 and 31-40.

这篇关于“块大小" Python的multiprocessing.Pool.map中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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