为什么`print`在Python多处理pool.map中不起作用

Rob*_*rto 5 python multiprocessing

我正在尝试实现该multiprocessing模块以使用大型csv文件.我正在使用Python 2.7并按照此处的示例进行操作.

我运行了未经修改的代码(为方便起见,下面复制了代码)并注意到函数中的print语句worker不起作用.无法print理解流程和调试.

任何人都可以解释为什么print不在这里工作?pool.map不执行打印命令吗?我在网上搜索但没有找到任何表明这一点的文件.

import multiprocessing as mp
import itertools
import time
import csv

def worker(chunk):
    # `chunk` will be a list of CSV rows all with the same name column
    # replace this with your real computation
    print(chunk)     # <----- nothing prints
    print 'working'  # <----- nothing prints
    return len(chunk)  

def keyfunc(row):
    # `row` is one row of the CSV file.
    # replace this with the name column.
    return row[0]

def main():
    pool = mp.Pool()
    largefile = 'test.dat'
    num_chunks = 10
    results = []
    with open(largefile) as f:
        reader = csv.reader(f)
        chunks = itertools.groupby(reader, keyfunc)
        while True:
            # make a list of num_chunks chunks
            groups = [list(chunk) for key, chunk in
                      itertools.islice(chunks, num_chunks)]
            if groups:
                result = pool.map(worker, groups)
                results.extend(result)
            else:
                break
    pool.close()
    pool.join()
    print(results)

if __name__ == '__main__':
    main()
Run Code Online (Sandbox Code Playgroud)

Blc*_*ght 8

这是IDLE的问题,您正在使用它来运行代码.IDLE对终端进行相当基本的仿真,以处理您在其中运行的程序的输出.它虽然无法处理子进程,所以虽然它们在后台运行得很好,但你永远不会看到它们的输出.

最简单的解决方法是从命令行运行代码.

另一种方法可能是使用更复杂的IDE.虽然我不确定哪些有更好的终端仿真用于多处理输出,但是在Python维基上列出了一堆.