1
Python 2.7でCSVファイルを処理するときに、ThreadPool
とGenerator
を使用するときに問題があります。ここに私のポイントを説明するいくつかのサンプルコードは次のとおりです。GeneratorでThreadPoolを正しく使用する
from multiprocessing.dummy import Pool as ThreadPool
import time
def getNextBatch():
# Reads lines from a huge CSV and yields them as required.
for i in range(5):
yield i;
def processBatch(batch):
# This simulates a slow network request that happens.
time.sleep(1);
print "Processed Batch " + str(batch);
# We use 4 threads to attempt to aleviate the bottleneck caused by network I/O.
threadPool = ThreadPool(processes = 4)
batchGenerator = getNextBatch()
for batch in batchGenerator:
threadPool.map(processBatch, (batch,))
threadPool.close()
threadPool.join()
私はこれを実行すると、私が期待される出力を得る:
バッチ1バッチ処理加工
バッチ処理0
処理バッチ3
バッチ処理4
問題は、各プリント間1秒の遅延と表示されることです。効果的には、私のスクリプトは順番に実行されています(複数のスレッドを使用しないでください)。
ここでの目標は、印刷されたステートメントをすべて1秒間に1秒間で表示するのではなく、5秒間表示することです。