SoFunction
Updated on 2025-03-01

A brief discussion on the usage and differences between threading join and setDaemon in Python

When programming multi-threaded Python, join() and setDaemon() methods are often used. Today I specially studied the difference between the two.

1. Join () method: In main thread A, child thread B is created and () is called in main thread A. Then, main thread A will wait at the call place and can only continue to execute after child thread B completes the operation. Then, when calling this thread, the join method of the called thread can be used.

Prototype: join([timeout])

The parameters inside are optional, representing the maximum time for the thread to run. That is, if this time exceeds this time, regardless of whether the thread has been executed or not, it will be recycled, and the main thread or function will continue to execute.

example:

import threading 
import time 
class MyThread(): 
  def __init__(self,id): 
    .__init__(self) 
     = id 
  def run(self): 
    x = 0 
    (10) 
    print  
 
if __name__ == "__main__": 
  t1=MyThread(999) 
  () 
  for i in range(5): 
    print I

The result after execution is:

0
1
2
3
4
999

When running on the machine, there is a noticeable pause between 4 and 999.

explain:

After thread t1 starts, the main thread does not wait for thread t1 to run before executing, but first completes the 5 loop printing (print to 4), and then sleep (10), thread t1 prints out the 999 passed in.

Now, we add the join() method (other code remains unchanged) to see what's different, example:

import threading 
import time 
class MyThread(): 
  def __init__(self,id): 
    .__init__(self) 
     = id 
  def run(self): 
    x = 0 
    (10) 
    print  
 
if __name__ == "__main__": 
  t1=MyThread(999) 
  () 
  () 
  for i in range(5): 
    print I 

The result after execution is:

999
0
1
2
3
4

There was a noticeable pause before 999 when running on the machine.

explain:

After thread t1 starts, the main thread stops at the join() method. After sleep (10), the thread t1 operation ends and is joined. Then, the main thread continues to print loops.

2. SetDaemon() method. In main thread A, child thread B is created and () is called in main thread A. This means that main thread A is set as the daemon thread. At this time, if main thread A is executed, regardless of whether child thread B is completed, it will exit with main thread A. This is the meaning of the setDaemon method, which is basically the opposite of join. In addition, there is another thing to pay attention to: it must be set before the start() method is called. If it is not set as a daemon thread, the program will be suspended infinitely.

Example: Setting the child thread to end with the end of the main thread:

import threading 
import time 
class MyThread(): 
  def __init__(self,id): 
    .__init__(self) 
  def run(self): 
    (5) 
    print "This is " + () 
 
if __name__ == "__main__": 
  t1=MyThread(999) 
  (True) 
  () 
  print "I am the father thread." 

The result after execution is:

I am the father thread.

It can be seen that the content in the child thread t1 has not been typed.

Explanation: (True) operation, set the parent thread as the daemon thread. According to the meaning of the setDaemon() method, the parent thread ends after printing the content, regardless of whether the child thread has completed execution.

During the program running, a main thread is executed. If the main thread creates another child thread, the main thread and the child thread will be divided into two groups and run separately. Then when the main thread completes and wants to exit, it will check whether the child thread is completed.

If the child thread does not complete, the main thread will wait for the child thread to complete before exiting.

But sometimes what we need is that as long as the main thread completes, regardless of whether the child thread completes, it must exit with the main thread. At this time, you can use the setDaemon method.

Therefore, the difference between join and setDaemon is as in the above example, and they are basically the opposite.

The above brief discussion on the usage and differences between threading join and setDaemon in Python is all the content I share with you. I hope you can give you a reference and I hope you support me more.