End sequence of the main thread and the child thread python Examples Analytical

This article describes the main thread and the end of the sequence of sub-thread python Resolves instances, the paper sample code described in great detail, has a certain reference value of learning for all of us to learn or work, a friend in need can refer to
this article main describes the end of the sequence python main thread and the child thread Resolves instances, the paper describes in great detail by the example code, a certain reference value of learning for all of us to learn or work, a friend in need can refer

Reference autonomous sub-thread of the thread exits the impact of the words:

For the program, if the main process when the child process is not finished already quit, then the parent of the Linux kernel will be changed to the child process ID 1 (init process that is), when the end will come to the child by the init process the recovery of the child.

After the main thread child thread exit status depends on it in the process, if the process does not quit, then the child thread is still running. If the process exits, then it will withdraw all threads, so the child thread will quit.

The main thread exits, the process waits for all child thread is finished before the end of
the process started by default produce a main thread, the main thread to create a child thread default case is not a daemon thread (setDaemon (False)). Therefore, after the end of the main thread, the child thread will continue execution, the process will wait for all children before the end of the thread is finished

All threads share a terminal output (terminal thread belongs processes)

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...
...

Visible After the parent thread, the child thread is still running, then the end of the process, the child thread will be terminated

After the main thread does not wait for the process to complete a daemon thread, ended immediately

When setting up a thread as a daemon thread, this thread belongs to the process will not wait for this thread to finish, the process will end immediately

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 non-daemon thread, so the process will wait after the end of thread2 completed, without waiting for the completion of thread1

Note: sub-thread inherits parent thread daemon of value, that is the guardian of the child thread is still open thread is a daemon thread

The main thread waits for the child after the end of the thread to complete

A thread in use B.join () indicates that the thread is blocked A call to join () at, and to wait to complete before thread B continues

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 in the implementation of the thread2.join (), wait until after the implementation of the next sentence thread2

1/0 error causes the main thread exits, and thread1 set up a daemon = True, and therefore the main thread accident thread1 will end immediately upon exiting. thread1.join () is not the main thread executed
recommend our learning Python buckle qun: 774711191, look at how seniors are learning! From basic web development python script to, reptiles, django, data mining, etc. [PDF, actual source code], zero-based projects to combat data are finishing. Given to every little python partner! Every day, Daniel explain the timing Python technology, to share some of the ways to learn and need to pay attention to small details, click on Join us python learner gathering

Published 35 original articles · won praise 9 · views 10000 +

Guess you like

Origin blog.csdn.net/haoxun02/article/details/104254627