[python tutorial] detailed tutorial on join() in multithreading

In Python's multi-threaded programming, we often encounter code such as thread.join(). Today, let's use the actual code to explain the function of the join function.

I
When a process is started, a main thread will be generated by default, because the thread is the smallest unit of the program execution flow. When multi threading is set, the main thread will create multiple sub threads. In python, by default (actually setDaemon(False)), the main thread exits after executing its own task, and the sub thread will continue to execute its own task, Until the end of your task, see the example below.

# Test the function of join in multithreading
import threading, time
def doWaiting():
    print 'start waiting1: ' + time.strftime('%H:%M:%S') + "\n"
    time.sleep(3)
    print 'stop waiting1: ' + time.strftime('%H:%M:%S') + "\n"
def doWaiting1():
    print 'start waiting2: ' + time.strftime('%H:%M:%S') + "\n"
    time.sleep(8)
    print 'stop waiting2: ', time.strftime('%H:%M:%S') + "\n"
tsk = []
thread1 = threading.Thread(target = doWaiting)
thread1.start()
tsk.append(thread1)
thread2 = threading.Thread(target = doWaiting1)
thread2.start()
tsk.append(thread2)
print 'start join: ' + time.strftime('%H:%M:%S') + "\n"

print 'end join: ' + time.strftime('%H:%M:%S') + "\n"

The code execution results are as follows:

start waiting1: 22:24:20

start join: 22:24:20
start waiting2: 22:24:20


end join: 22:24:20

stop waiting1: 22:24:23

stop waiting2:  22:24:28

II
When we use the setDaemon(True) method to set the child thread as the guardian thread, once the main thread finishes executing, all threads will be terminated. The possible situation is that the child thread's task will be forced to stop before it has finished executing completely. See the following example.

# Test the function of join in multithreading
'''
No one answers the problems encountered in learning? Xiaobian created a Python exchange of learning QQ Group: 531509025
 Look for like-minded friends to help each other,There are also good videos and tutorials in the group PDF e-book!
'''
import threading, time
def doWaiting():
    print 'start waiting1: ' + time.strftime('%H:%M:%S') + "\n"
    time.sleep(3)
    print 'stop waiting1: ' + time.strftime('%H:%M:%S') + "\n"
def doWaiting1():
    print 'start waiting2: ' + time.strftime('%H:%M:%S') + "\n"
    time.sleep(8)
    print 'stop waiting2: ', time.strftime('%H:%M:%S') + "\n"
tsk = []
thread1 = threading.Thread(target = doWaiting)
thread1.setDaemon(True)
thread1.start()
tsk.append(thread1)
thread2 = threading.Thread(target = doWaiting1)
thread2.setDaemon(True)
thread2.start()
tsk.append(thread2)
print 'start join: ' + time.strftime('%H:%M:%S') + "\n"

print 'end join: ' + time.strftime('%H:%M:%S') + "\n"

The code execution results are as follows:

start waiting1: 22:34:46

start waiting2: 22:34:46
start join: 22:34:46


end join: 22:34:46

III
When the daemon thread is not set and the timeout parameter of the join function is not set, the main thread will wait until all the sub threads end, the main thread ends and the program exits.

The code is as follows:

# Test the function of join in multithreading
import threading, time
def doWaiting():
    print 'start waiting1: ' + time.strftime('%H:%M:%S') + "\n"
    time.sleep(3)
    print 'stop waiting1: ' + time.strftime('%H:%M:%S') + "\n"
def doWaiting1():
    print 'start waiting2: ' + time.strftime('%H:%M:%S') + "\n"
    time.sleep(8)
    print 'stop waiting2: ', time.strftime('%H:%M:%S') + "\n"
tsk = []
thread1 = threading.Thread(target = doWaiting)
thread1.start()
tsk.append(thread1)
thread2 = threading.Thread(target = doWaiting1)
thread2.start()
tsk.append(thread2)
print 'start join: ' + time.strftime('%H:%M:%S') + "\n"
for tt in tsk:
    tt.join()
print 'end join: ' + time.strftime('%H:%M:%S') + "\n"

The code execution results are as follows:

start waiting1: 22:41:50

start join: 22:41:50
start waiting2: 22:41:50


stop waiting1: 22:41:53

stop waiting2:  22:41:58

end join: 22:41:58

IV
When the daemon thread is not set and the timeout parameter of the join function is 2, the main thread will wait for the accumulation of the timeout of multiple sub threads and such a period of time. When the time comes, the main thread ends, but the sub threads are not killed. The sub threads can still continue to execute until all the sub threads end and the program exits.

The code is as follows:

# Test the function of join in multithreading
'''
No one answers the problems encountered in learning? Xiaobian created a Python exchange of learning QQ Group: 531509025
 Look for like-minded friends to help each other,There are also good videos and tutorials in the group PDF e-book!
'''
import threading, time
def doWaiting():
    print 'start waiting1: ' + time.strftime('%H:%M:%S') + "\n"
    time.sleep(3)
    print 'stop waiting1: ' + time.strftime('%H:%M:%S') + "\n"
def doWaiting1():
    print 'start waiting2: ' + time.strftime('%H:%M:%S') + "\n"
    time.sleep(8)
    print 'stop waiting2: ', time.strftime('%H:%M:%S') + "\n"
tsk = []
thread1 = threading.Thread(target = doWaiting)
thread1.start()
tsk.append(thread1)
thread2 = threading.Thread(target = doWaiting1)
thread2.start()
tsk.append(thread2)
print 'start join: ' + time.strftime('%H:%M:%S') + "\n"
for tt in tsk:
    tt.join(2)
print 'end join: ' + time.strftime('%H:%M:%S') + "\n"

The code execution results are as follows:

start waiting1: 23:02:34

start waiting2: 23:02:34
start join: 23:02:34


stop waiting1: 23:02:37

end join: 23:02:38

stop waiting2:  23:02:42

V
When the parameter timeout=2 of the daemon thread join function is set, the main thread will wait for the accumulation of the timeout of multiple child threads and such a period of time. When the time comes, the main thread ends, kills the unfinished child threads, and the program exits.

The code is as follows:

# Test the function of join in multithreading
'''
No one answers the problems encountered in learning? Xiaobian created a Python exchange of learning QQ Group: 531509025
 Look for like-minded friends to help each other,There are also good videos and tutorials in the group PDF e-book!
'''
import threading, time
def doWaiting():
    print 'start waiting1: ' + time.strftime('%H:%M:%S') + "\n"
    time.sleep(3)
    print 'stop waiting1: ' + time.strftime('%H:%M:%S') + "\n"
def doWaiting1():
    print 'start waiting2: ' + time.strftime('%H:%M:%S') + "\n"
    time.sleep(8)
    print 'stop waiting2: ', time.strftime('%H:%M:%S') + "\n"
tsk = []
thread1 = threading.Thread(target = doWaiting)
thread1.setDaemon(True)
thread1.start()
tsk.append(thread1)
thread2 = threading.Thread(target = doWaiting1)
thread2.setDaemon(True)
thread2.start()
tsk.append(thread2)
print 'start join: ' + time.strftime('%H:%M:%S') + "\n"
for tt in tsk:
    tt.join(2)
print 'end join: ' + time.strftime('%H:%M:%S') + "\n"

The code execution results are as follows:

start waiting1: 23:23:57

start waiting2: 23:23:57
start join: 23:23:57


stop waiting1: 23:24:00

end join: 23:24:01

Keywords: Python

Added by mjdamato on Fri, 24 Sep 2021 10:15:15 +0300