1

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.

0

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.