What's the correct way to join threads in Python multiprocessing and how these differ?

50 views Asked by At

What's the correct way to join threads in multiprocessing, so the main program waits the completion of all threads before going ahead?

I have seen these 2 ways while researching, not sure how they differ especially the first one as the second one seems more common:

for i in range(100):
    j = Process(target=somefunc,args=(i,))
    j.start()

j.join()

And

jobs = []
for i in range(100):
    j = Process(target=somefunc,args=(i,))
    jobs.append(j)
    j.start()

for j in jobs:
    j.join()

I am on Python 2.6.6 due to limitations on the server.

1

There are 1 answers

0
mkrieger1 On BEST ANSWER

The first way is not correct, because it only waits for the last process that was created. It cannot wait for all other processes, because it didn't keep a reference to them before starting the next process.

The second way is correct, because it collects references to all started processes in a list and then waits for all of them.