为什么`print`在Python多处理pool.map中不起作用
我正在尝试实现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()
-
这是IDLE的问题,您正在使用它来运行代码。IDLE对终端进行了相当基本的仿真,以处理您在其中运行的程序的输出。但是它无法处理子流程,因此尽管它们会在后台正常运行,但您永远看不到它们的输出。
最简单的解决方法是仅从命令行运行代码。
另一种选择是使用更复杂的IDE。Python
Wiki上列出了一堆,尽管我不确定哪个对多处理输出具有更好的终端仿真。