python thread join

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

Intelligent Recommendation

Join and Daemon in Python multi-thread

Article catalog First Off: Simple Join () The second level: Join (Timeout) Third level: setDaemon (TRUE) First Off: Simple Join () The execution result is: In order to figure out the role of join (), ...

Python Thread object join method and daemon thread

1 Introduction When I first came into contact with multithreaded programming, for daemon threads andThread Objectjoin() The method is not very clear. After a period of study and thinking, it is now ro...

Process, thread, join, daemon thread in python

Hard disk speed is the slowest, memory speed is fast, cpu speed is super fast It seems that the single core can only do one thing at the same time, but through the context switch of the cpu (understan...

Thread ------ join thread join()

The join() method must be after the stat() method in order to work result...

Python multi-thread (2) join function

Write a multi-threaded program first operation result: It can be found that all done is output first, and then T1 finish is output. What if you want to wait for the T1 thread to finish executing and t...

More Recommendation

Python multithreading join method and daemon thread

Great god please detour Comparison of Python multithreading and multiprocessing When using Python, when is it suitable to use multithreading? When is it suitable to use multiple processes? For io oper...

Python thread join method and seDaemon method

Preface Based on the previous article, we understand the python program execution flow, why use threads, and under what circumstances to use python threads, this article will talk about the join() and...

Python- multithreading blocking (join) and daemon thread (setDaemon)

Preface Today, Xiao Wang invited Xiaoming and Xiaowang to eat hot pot. After the hot pot, there will be the following three scenes: Scene 1: Xiao Wang (main) has finished eating first, Haihai (guest) ...

Python - Basic Knowledge Point - Thread Join, SetDaemon

1. join Used to block the main thread, until the sub-thread is completed or blocked the main thread, the main thread continues to perform. 2. setDaemon The sub-thread is set to the daemon. When the ma...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top