2

just as the sample code shows:

for temp in range(0, 10):
       thread = threading.Thread(target = current_post.post)
       thread.start()
       threads.append(thread)

for current in range(10):
        threads[current].join()

the code is just a part of a python file, but it stands for most circumstances: i should execute join() after start() in multithreading. i have been confusing by it for a few days. as we all know, when we execute thread.start(), a new thread then starts, python runs through different threads automatically, thats all we need. if so, why should i add thread.join() after start()? join() means waiting until the current thread finished IMO. but does it means a kind of single-thread? i have to wait each thread to finish their tasks, its not multithreading! join() only means executing the specified function one by one IMO. cannot start() finish the multi-threading perfectly? why should i add join() function to let them finish one by one? thx for any help :)

1
  • A more Pythonic way to write the example is to replace temp with _ (a common idiom for unused temporary variables), and to iterate over the threads list directly (for thread in threads) instead of indexing into it. Commented Jun 18, 2015 at 21:17

2 Answers 2

6

You do it in order to be sure that your threads have actually finished (and do not become, for example, zombie processes, after your main thread exits).

However, you don't have to do it right after starting the threads. You can do it at the very end of your process.

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

Comments

1

Join will block the current thread until the thread upon which join is called has finished.

Essentially your code is starting a load of threads and then waiting for them all to complete.

If you didn't then the chances are the process would exit and none of your threads would do anything.

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.