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%にしか到達しません。