多处理imap suprress子进程打印

时间:2016-01-29 16:18:03

标签: python multiprocessing

虽然我在Jupyter笔记本(python3.5)中运行以下代码:

def process(doc):
    print('Process Hey!')
with Pool() as pool:
    results = pool.imap(process, docs)

这不会给笔记本打印,但如果我尝试使用pool.map,它会吐出来的话。所以我真的很好奇这里发生了什么。谢谢!

1 个答案:

答案 0 :(得分:1)

从文档imap A lazier version of map()imap返回迭代器map返回一个列表:

In [24]:  with Pool() as pool:
            results = pool.map(process, docs)
            print(type(results))
   ....:     
Process Hey!
Process Hey!
<class 'list'>


In [25]:  with Pool() as pool:
                results = pool.imap(process, docs)
                print(type(results))
   ....:     
<class 'multiprocessing.pool.IMapIterator'>

In [27]:  with Pool() as pool:
                results = pool.imap(process, docs)
                for _ in results:
                     pass
   ....:     
Process Hey!
Process Hey!
In [28]:  with Pool() as pool:
            results = pool.imap(process, docs)
            list(results)
 ....:     
Process Hey!
Process Hey!

它相当于python2中itertools.imapmap之间的差异,即懒惰评估与贪婪评估:

In [3]: from itertools import imap

In [4]: r = imap(lambda x: x+1, [1,2,3])
In [4]: r
Out[5]: <itertools.imap at 0x7f09170f1e10>
In [6]: list(r)
Out[6]: [2, 3, 4]

In [7]: r = map(lambda x: x+1, [1,2,3])    
In [8]: r
Out[8]: [2, 3, 4]