Here is a bit of code I've run. I'm trying to make sure that all my processes finish before I move on through my code, but it's not happening as I expect.
import sys
import multiprocessing as mp
import time
import random
def testJoin(argin):
name = mp.current_process().name
exStartTime = time.time()
time.sleep(argin * random.random())
print(name + ' took %d seconds' %(time.time() - exStartTime))
sys.stdout.flush()
if __name__ == '__main__':
instances = [10, 10, 10, 10]
jobs = []
for k in instances:
p = mp.Process(target = testJoin, args = (k,))
jobs.append(p)
p.start()
p.join()
print('End of Program')
Here is what's being output:
End of Program
Process-4 took 1 seconds
End of Program
End of Program
Process-2 took 4 seconds
End of Program
Process-1 took 9 seconds
End of Program
Process-3 took 9 seconds
Where I'm confused is that I don't expect to see "End of Program" printed more than once, and I certainly don't expect to see it printed until all of my four processes have ended. What am I missing?