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)