A few facts
After 1 python default parameters to create threads, regardless of whether the main thread is finished, will be waiting for child thread is finished with only exit, whether as the result of join
2 If you create a thread, and the daemon is set to true, thread.setDaemon (True), then automatically exit after the main thread is finished, the results will not wait for the child threads. And with the main thread exits, the child thread is also perish.
3 join method of action is blocked, waiting for the child thread ends, join a method parameter is the timeout, that is, if the main thread waits for timeout, the child thread has not ended, the main thread is forced to end child thread.
4 If the thread daemon property to False, then join in the timeout parameter is invalid. The main thread will wait until the child thread ends.
5 If the thread daemon property is True, then join in the timeout parameter is valid, the main thread waits timeout after time, end child thread. Here there is a pit, that is, if there are N sub-thread join (timeout) at the same time, then in fact the main thread will wait for the timeout period of up to N * timeout, timeout because the start time of each sub-sub-thread is a thread on the end of overtime moment.
Test code
import threading,time
def func():
print "start thread time: ",time.strftime('%H:%M:%S')
time.sleep(3)
print "stop thread time: ",time.strftime('%H:%M:%S')
thread_list = []
for i in range(3):
t1 = threading.Thread(target=func)
#t1.setDaemon(True)
thread_list.append(t1)
for r in thread_list:
r.start()
for t in thread_list:
#t.join(1)
t.join()
print "stop main thread"
If the child thread ### is different t.join (timeout) according timeout, the result will be different, the premise is set setDaemon (True), or join the timeout is not valid
# Set the setDaemon (True), but did not set the operating results t.join () is:
#start thread time: 17:25:29
#start thread time: 17:25:29
#start thread time: 17:25:29
#stop main thread
# Added t1.setDaemon (True), and set a timeout period operating results t.join (1) of:
#start thread time: 17:12:24
#start thread time: 17:12:24
#start thread time: 17:12:24
#stop main thread
# Did not add t1.setDaemon (True), and set a timeout period operating results t.join (1), but because setDaemon True parameter is not set timeout so even if useless:
#start thread time: 17:13:28
#start thread time: 17:13:28
#start thread time: 17:13:28
#stop main thread
#stop thread time: 17:13:31
#stop thread time: 17:13:31
#stop thread time: 17:13:31
# Did not add t1.setDaemon (True), but set t.join (), no timeout blocking operating results:
#start thread time: 17:16:12
#start thread time: 17:16:12
#start thread time: 17:16:12
#stop thread time: 17:16:15
#stop thread time: 17:16:15
#stop thread time: 17:16:15
#stop main thread
# That does not set setDaemon (True), there is no set join () operating results:
#start thread time: 17:22:25
#start thread time: 17:22:25
#start thread time: 17:22:25
#stop main thread
#stop thread time: 17:22:28
#stop thread time: 17:22:28
#stop thread time: 17:22:28
to sum up:
If you want the child process normal end of the run (all the contents of child processes are running), then if you set join (timeout), then the premise is set setDaemon (True), and setDaemon parameter is True, and join (timeout) the timeout must be greater than the time required to execute the child, or the child did not wait for the end of the process running on super
When to quit. Or directly join () without a timeout, they do not set setDaemon (True) a