Few facts
1 python default parameters after the thread is created, regardless of whether the main thread is executed, will wait for the child thread to complete the execution before exiting, with or without join results
2 If the thread is created and the daemon is set to true, that is, thread.setDaemon(True), the main thread will automatically exit after execution, and will not wait for the execution result of the child thread. And as the main thread exits, the child thread also dies.
3 The role of the join method is blocking, waiting for the end of the child thread, the join method has a parameter is timeout, that is, if the main thread waits for timeout, the child thread has not finished, the main thread forcibly ends the child thread.
4 If the thread daemon attribute is False, the timeout parameter in the join is invalid. The main thread will wait for the end of the child thread.
5 If the thread daemon attribute is True, the timeout parameter in the join is valid. The main thread will wait for the timeout time and end the child thread. There is a pit here, that is, if there are N child threads join (timeout) at the same time, then the main thread will wait for the maximum timeout of N * timeout, because each child thread's timeout start time is the last child thread timeout. Moment.
Test code and run results:
import threading, time
def doThreadTest():
print 'start thread time:', time.strftime('%H:%M:%S')
time.sleep(10)
print 'stop thread time:', time.strftime('%H:%M:%S')
threads = []
for i in range(3):
thread1 = threading.Thread(target = doThreadTest)
thread1.setDaemon(True)
threads.append(thread1)
for t in threads:
t.start()
for t in threads:
t.join(1)
print 'stop main thread'
If comment out thread1.setDaemon(True), the result is
join() Create a thread (thread name THREAD-1) using Thread, which performs the above TEST () function. IFname == ‘main': The following is the main thread, the main thread will continue to perfor...
When a process starts, it will generate a main thread by default, because the thread is the smallest unit of the program execution flow. When setting up multiple threads, the main thread will create m...
Runtime when using multiple processes without blocking with the join function 2, when only one join is called, the main process that is called will wait for the child process to continue to execute do...
The effect of the join() method in Python multithreading and multiprocessing is the same. Take multithreading as an example: The work done by join is thread synchronization, that is, after the task of...
Article Directory When no join is added, the relationship between the main thread and the child thread is verified Code operation result When using join, the relationship between the main thread and t...
1. Questions raised Always report errors when running the program 2. Problem solving Locate the tqdm package you installed, usually installed in site-packages/tqdm, then find the _monitor.py file, ope...
The execution of the main thread is completed, the sub-thread will continue to execute and the program will end When t.setDaemon(True) starts the daemon process, the main thread execution is completed...
When executing the above program, you will find that after the main thread exits, the child threads are still executing, and the process ends after all the child threads are executed. Insert join() af...
A code Two running results E:\python\python can learn this way\Chapter 13 Multithreading and Multiprocess Programming\code>python SecondExample.py 15 16 17 18 19 5 6 7 8 9 True True...
Write a custom directory title here The first: Python multi-thread default (set thread setdaemon (false)), after the main thread performs your own task, then quit, at which time the sub-line continues...