我有一个脚本可以通过imap_unordered()
调用成功完成多处理池任务集:
p = multiprocessing.Pool()
rs = p.imap_unordered(do_work, xrange(num_tasks))
p.close() # No more work
p.join() # Wait for completion
但是,我num_tasks
大约是250,000,因此join()
锁将主线程锁定了10秒钟左右,我希望能够逐步回显命令行以显示主进程未锁定。就像是:
p = multiprocessing.Pool()
rs = p.imap_unordered(do_work, xrange(num_tasks))
p.close() # No more work
while (True):
remaining = rs.tasks_remaining() # How many of the map call haven't been done yet?
if (remaining == 0): break # Jump out of while loop
print "Waiting for", remaining, "tasks to complete..."
time.sleep(2)
是否有用于结果对象或池本身的方法来指示剩余任务数?我尝试使用multiprocessing.Value
对象作为计数器(在完成任务后do_work
调用counter.value += 1
操作),但是在停止递增之前,计数器只能达到总值的〜85%。