9

I have a query. I have seen examples where developers write something like the code as follows:

import threading
def do_something():
    return true
t = threading.Thread(target=do_something)
t.start()
t.join()

I know that join() signals the interpreter to wait till the thread is completely executed. But what if I do not write t.join()? Will the thread get closed automatically and will it be reused later?
Please let me know the answer. It's my first attempt at creating a multi-threaded application in Python 3.5.0.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139

3 Answers3

12

A Python thread is just a regular OS thread. If you don't join it, it still keeps running concurrently with the current thread. It will eventually die, when the target function completes or raises an exception. No such thing as "thread reuse" exists, once it's dead it rests in peace.

Unless the thread is a "daemon thread" (via a constructor argument daemon or assigning the daemon property) it will be implicitly joined for before the program exits, otherwise, it is killed abruptly.

One thing to remember when writing multithreading programs in Python, is that they only have limited use due to infamous Global interpreter lock. In short, using threads won't make your CPU-intensive program any faster. They can be useful only when you perform something involving waiting (e.g. you wait for certain file system event to happen in a thread).

bereal
  • 32,519
  • 6
  • 58
  • 104
  • 2
    Please let me know whether the thread get released even I am not writing the `.join()`? Will it affect the programming logic?Will it make the process slow? – Jaffer Wilson Feb 10 '20 at 10:23
  • @JafferWilson It's not clear what you mean by releasing a thread. It will be destroyed eventually. It certainly _might_ affect the program logic depending on how you access shared resources. – bereal Feb 10 '20 at 10:30
  • I mean will it impact the program badly. I mean will it make the program work slower than what it was previously? Or any other side effects of not using the `join()`? – Jaffer Wilson Feb 10 '20 at 10:37
  • @JafferWilson if you're creating just one thread and then join it, it is quite the same as just calling the target function. If you're not joining it, then it all depends on what actually happens in the target function. I updated the question with some performance-related concerns. – bereal Feb 10 '20 at 10:43
  • 2
    @bereal, Re, "not clear what you mean by releasing a thread." That's the heart of the question. In a typical OS, after a thread has "died," it continues to use some system resources (e.g., a thread-ID and/or a "descriptor") until the program explicitly releases them. In some programming systems (e.g., Java) the library automatically releases the dead thread, and `join()` literally does nothing except wait for the thread to "die." In others (e.g., C++), releasing the descriptor happens inside the `join()` call. OP is asking which way it works in Python. (Sorry OP, I personally don't remember.) – Solomon Slow Feb 10 '20 at 15:24
  • so even if we don't join thread. the thread will be deleted after the function completes? – Mojtaba Arezoomand Oct 10 '21 at 13:24
  • 1
    @MojixCoder yes, if you mean the thread target function. – bereal Oct 11 '21 at 05:49
  • no I meant the actual thread. even if we don't `join()` thread. will it be closed? @bereal – Mojtaba Arezoomand Oct 11 '21 at 09:42
  • 1
    @MojixCoder sorry, I don't understand. Thread terminates when it function exits. Or when the process ends (if it's a daemon thread). – bereal Oct 11 '21 at 09:50
  • so why we do use `.join()` on threads? just to wait for their task to finish? is it okay not to use `.join()` on threads when we don't want to wait? @bereal – Mojtaba Arezoomand Oct 11 '21 at 09:53
  • @MojixCoder yes, it's normally ok not to use `join()` if you don't want to wait. – bereal Oct 11 '21 at 10:07
  • "A Python thread is just a regular OS thread." is this not misleading? Then, what is a Python "Process"? – mehmet Dec 08 '21 at 07:04
  • @mehmet no, it's not. A Python "process" is a regular OS process. – bereal Dec 08 '21 at 07:33
  • @bereal thanks! earlier I was thinking python threads had no appearance in OS. I actually came here to delete the comment, but you beat me. – mehmet Dec 08 '21 at 07:35
8

The join part means the main program will wait for the thread to end before continuing. Without join, the main program will end and the thread will continue.

Now if you set the daemon parameter to "True", it means the thread will depends on the main program, and it will ends if the main program ends before.

Here is an example to understand better :

import threading
import time

def do_something():
    time.sleep(2)
    print("do_something")
    return True

t = threading.Thread(target=do_something)
t.daemon = True # without the daemon parameter, the function in parallel will continue even your main program ends
t.start()
t.join() # with this, the main program will wait until the thread ends
print("end of main program")

no daemon, no join:

end of main program
do_something

daemon only:

end of main program

join only:

do_something
end of main program

daemon and join:

do_something
end of main program
# Note : in this case the daemon parameter is useless
Phoenixo
  • 2,071
  • 1
  • 6
  • 13
  • This doesn't help, especially since you're setting `.daemon = True`, which changes how threads work when the main program is quitting. – AKX Feb 10 '20 at 10:20
1
  • Without join(), non-daemon threads are running and are completed with the main thread concurrently.

  • Without join(), daemon threads are running with the main thread concurrently and when the main thread is completed, the daemon threads are exited without completed if the daemon threads are still running.

You can see my answer in this post explaining about it in detail.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129