我有一个可以运行multiprocessing的脚本pool.map。问题是并非所有进程都需要很长时间才能完成,因此某些进程会休眠,因为它们会等到所有进程完成(与此问题相同的问题)。有些文件在不到一秒的时间内完成,其他文件则需要几分钟(或几小时)。
如果我正确理解手册(和这篇文章)pool.imap ,则不会等待所有进程完成,如果完成一个进程,它会提供一个新文件来处理。当我尝试这样做时,脚本正在加速要处理的文件,小文件按预期处理,大文件(需要更多时间处理)直到最后才完成(在没有通知的情况下被杀死?)。这是正常行为pool.imap,还是我需要添加更多命令/参数?time.sleep(100)当我将部分添加else为测试时,它正在处理更大的文件,但其他进程会进入睡眠状态。有什么建议 ?谢谢
def process_file(infile):
#read infile
#compare things in infile
#acquire Lock, save things in outfile, release Lock
#delete infile
def main():
#nprocesses = 8
global filename
pathlist = ['tmp0', 'tmp1', 'tmp2', 'tmp3', 'tmp4', 'tmp5', 'tmp6', 'tmp7', 'tmp8', 'tmp9']
for d in pathlist:
os.chdir(d)
todolist = []
for infile in os.listdir():
todolist.append(infile)
try:
p = Pool(processes=nprocesses)
p.imap(process_file, todolist)
except KeyboardInterrupt:
print("Shutting processes down")
# …Run Code Online (Sandbox Code Playgroud)