2016-04-18 19 views
1

私は入力ファイルからデータを読み込もうとしており、各行に対してwhileループでタスクを実行しています。問題は、最初のプロセスを作成するときに、そのループが実行されており、上記のforループに制御を戻していないことです。ボトムラインは並列性がない。私は間違って何をしていますか?ここでPython forループを並列に処理する

は、関連するコードです:

from multiprocessing import Process 


def work_line(list1Line,jobId): 
    while True: 
     print list1Line 
     tenant = list1Line[0] 
     module = list1Line[1] 
     endTime = int(time.time()) 
     startTime = endTime - startTimeDelta 
     generate(jobId, startTime, endTime, tenantServiceAddress, tenant, module) 
     print ("tenant {} will sleep for {} seconds").format(tenant,sleepBetweenLoops) 
     time.sleep(sleepBetweenLoops) 


def openFiles(): 
    file = open(CLOUD_INPUT_FILE, 'r') 
    lines = file.readlines() 
    file.close() 
    linesLen = len(lines) 
    processes = [] 

    for linesIndex in range(0, linesLen): 
     jobId = GenerateRandomID() 
     line = lines[linesIndex] 
     list1Line = line.split() 

     p = Process(target=work_line(list1Line,jobId)) 
     p.start() 
     processes.append(p) 
     print processes 

    for p in processes: 
     p.join() 


if __name__ == '__main__': 
    CLOUD_INPUT_FILE = r'C:\CF\input_file.txt' 
    tenantServiceAddress = 'address.address' 
    startTimeDelta = 300 
    sleepBetweenLoops = 1800 
    print multiprocessing.cpu_count() 
    openFiles() 

答えて

2

あなたが実際に関数を呼び出しています。

p = Process(target=work_line, args=(list1Line,jobId))