为什么`print`在Python multiprocessing pool.map中不起作用
我正在尝试使用 multiprocessing
模块来处理一个很大的 CSV 文件。我用的是 Python 2.7,并且参考了 这里 的例子。
我运行了未修改的代码(为了方便,下面复制了一遍),发现 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()
1 个回答
16
这是关于你用来运行代码的IDLE的问题。IDLE只是简单模拟了一个终端,用来处理你在里面运行程序的输出。但是,它不能处理子进程,所以虽然这些子进程在后台可以正常运行,但你永远看不到它们的输出。
最简单的解决办法就是直接从命令行运行你的代码。
另外一个选择是使用更高级的集成开发环境(IDE)。在Python的维基网站上有很多这样的IDE,不过我不太确定哪些能更好地处理多进程的输出。