Python main thread and sub-thread end sequence

For the program, if the main process exits before the child process ends, the Linux kernel will change the parent process ID of the child process to 1 (that is, the init process), and the init process will come when the child process ends. Recycle the child process.

After the main thread exits, the status of the child thread depends on the process it is in. If the process does not exit, the child thread still runs normally. If the process exits, all its threads will exit, so the child threads will exit.
The main thread exits, the process waits for all child threads to finish executing before ending

After the process is started, a main thread is generated by default. By default, the child threads created by the main thread are not daemon threads (setDaemon(False)). Therefore, after the main thread ends, the child threads will continue to execute, and the process will wait for all child threads to complete before ending

All threads share a terminal output (the terminal of the process to which the thread belongs)

import threading
import time

def child_thread1():
    for i in range(100):
        time.sleep(1)
        print('child_thread1_running...')

def parent_thread():
    print('parent_thread_running...')
    thread1 = threading.Thread(target=child_thread1)
    thread1.start()
    print('parent_thread_exit...')

if __name__ == "__main__":
    parent_thread()

The output is:

parent_thread_running...
parent_thread_exit...
child_thread1_running...
child_thread1_running...
child_thread1_running...
child_thread1_running...
...

It can be seen that after the parent thread ends, the child thread is still running. At this time, the process is terminated, the child thread will be terminated. After the
main thread ends, the process does not wait for the completion of the daemon thread, and ends immediately.

When a thread is set as a daemon thread, the process to which this thread belongs will not wait for the end of this thread, and the process will end immediately

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import threading
import time

def child_thread1():
    for i in range(100):
        time.sleep(1)
        print('child_thread1_running...')

def child_thread2():
    for i in range(5):
        time.sleep(1)
        print('child_thread2_running...')

def parent_thread():
    print('parent_thread_running...')
    thread1 = threading.Thread(target=child_thread1)
    thread2 = threading.Thread(target=child_thread2)
    thread1.setDaemon(True)
    thread1.start()
    thread2.start()
    print('parent_thread_exit...')

if __name__ == "__main__":
    parent_thread()

Output:

parent_thread_running...
parent_thread_exit...
child_thread1_running...child_thread2_running...

child_thread1_running...child_thread2_running...

child_thread1_running...child_thread2_running...

child_thread1_running...child_thread2_running...

child_thread2_running...child_thread1_running...

Process finished with exit code 0

Thread1 is a daemon thread, thread2 is not a daemon thread. Therefore, the process will wait for thread2 to finish, instead of waiting for thread1 to finish.

Note: The child thread will inherit the value of the daemon in the parent thread, that is, the child thread opened by the daemon thread is still the daemon thread. The
main thread waits for the completion of the child thread and ends.

Using B.join() in thread A means that thread A is blocked at calling join() and must wait for the completion of thread B to continue execution

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import threading
import time

def child_thread1():
    for i in range(10):
        time.sleep(1)
        print('child_thread1_running...')

def child_thread2():
    for i in range(5):
        time.sleep(1)
        print('child_thread2_running...')

def parent_thread():
    print('parent_thread_running...')
    thread1 = threading.Thread(target=child_thread1)
    thread2 = threading.Thread(target=child_thread2)
    thread1.setDaemon(True)
    thread2.setDaemon(True)
    thread1.start()
    thread2.start()
    thread2.join()
    1/0
    thread1.join()
    print('parent_thread_exit...')

if __name__ == "__main__":
    parent_thread()

Output:

parent_thread_running...
child_thread1_running...
child_thread2_running...
child_thread1_running...
child_thread2_running...
child_thread1_running...
child_thread2_running...
child_thread1_running...
child_thread2_running...
child_thread1_running...
child_thread2_running...
Traceback (most recent call last):
  File "E:/test_thread.py", line 31, in <module>
    parent_thread()
  File "E:/test_thread.py", line 25, in parent_thread
    1/0
ZeroDivisionError: integer division or modulo by zero

The main thread is blocked when thread2.join() is executed, and the next sentence will not be executed until thread2 ends.

1/0 will cause the main thread to report an error and exit, and thread1 sets daemon=True, so thread1 will end immediately when the main thread exits unexpectedly. thread1.join() is not executed by the main thread

Guess you like

Origin blog.csdn.net/sinat_38682860/article/details/108664934