Python 多处理:map 和 imap 有什么区别? [英] Python Multiprocessing: What's the difference between map and imap?

查看:97
本文介绍了Python 多处理:map 和 imap 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何使用 Python 的多处理包,但我不明白 mapimap 之间的区别.

I'm trying to learn how to use Python's multiprocessing package, but I don't understand the difference between map and imap.

map 返回一个实际的数组或集合,而 imap 返回一个数组或集合上的迭代器的区别是什么?我什么时候会使用一个?

Is the difference that map returns, say, an actual array or set, while imap returns an iterator over an array or set? When would I use one over the other?

另外,我不明白 chunksize 参数是什么.这是传递给每个进程的值的数量吗?

Also, I don't understand what the chunksize argument is. Is this the number of values that are passed to each process?

推荐答案

这就是区别.您可能使用 imap 而不是 map 的一个原因是,如果您想开始处理前几个结果而不等待其余的结果被计算出来.map 在返回之前等待每个结果.

That is the difference. One reason why you might use imap instead of map is if you wanted to start processing the first few results without waiting for the rest to be calculated. map waits for every result before returning.

对于块大小,有时分出大量工作更有效,因为每次工作程序请求更多工作时,都会产生 IPC 和同步开销.

As for chunksize, it is sometimes more efficient to dole out work in larger quantities because every time the worker requests more work, there is IPC and synchronization overhead.

这篇关于Python 多处理:map 和 imap 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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