import time multiprocessing import process def loop(limit): in xrange(limit): pass print limit = 100000000 #100 million start = time.time() in xrange(5): p = process(target=loop, args=(limit,)) p.start() p.join() end = time.time() print end - start i tried running code, output getting
99999999 99999999 2.73401999474 99999999 99999999 99999999 and sometimes
99999999 99999999 3.72434902191 99999999 99999999 99999999 99999999 99999999 in case loop function called 7 times instead of 5. why strange behaviour?
i confused role of p.join() statement. ending 1 process or of them @ same time?
the join function wait last process call finish before moving onto next section of code. if walk through have done should see why "strange" output.
for in xrange(5): p = process(target=loop, args=(limit,)) p.start() this starts 5 new processes 1 after other. these running @ same time. just @ least, down scheduler decide process being processed.
this mean have 5 processes running now:
process 1
process 2
process 3
process 4
process 5
p.join() this going wait p process finish process 5 last process assigned p.
lets process 2 finishes first followed process 5, feasible scheduler give processes more time on cpu.
process 1
process 2 prints 99999999
process 3
process 4
process 5 prints 99999999
the p.join() line move on next part p process 5 has finished.
end = time.time() print end - start this section prints part , there 3 processes still going on after output.
the other processes finish , print there 99999999.
to fix behaviour need .join() processes. alter code this...
processes = [] in xrange(5): p = process(target=loop, args=(limit,)) p.start() processes.append(p) process in processes: process.join() this wait first process, second , on. won't matter if 1 process finished before anther because every process on list must waited on before script continues.
Comments
Post a Comment