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

发布于 2021-01-29 17:02:40

我正在尝试实现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()
关注者
0
被浏览
139
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    这是IDLE的问题,您正在使用它来运行代码。IDLE对终端进行了相当基本的仿真,以处理您在其中运行的程序的输出。但是它无法处理子流程,因此尽管它们会在后台正常运行,但您永远看不到它们的输出。

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

    另一种选择是使用更复杂的IDE。Python
    Wiki
    上列出了一堆,尽管我不确定哪个对多处理输出具有更好的终端仿真。



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看