The difference and usage of threading join and Setdaemon in Python

Source: Internet
Author: User

The join () and Setdaemon () methods are often used in Python multithreaded programming, with the following basic usage:

Join ([TIME]): Waits until the thread aborts. This blocks the calling thread until the thread's join () method is called abort-gracefully exits or throws an unhandled exception-or an optional timeout occurs. Setdaemon, marking the thread as a daemon thread or user thread

1, Join () method: The main thread A, the child thread B is created, and in the main thread A is called B.join (), then the main thread A will wait in the place of the call, until sub-thread B completes the operation, can then proceed, then the call of the thread can use the join method of the called Thread.
Prototype: Join ([timeout]), the parameters in the optional, the maximum time for the thread to run, that is, if the time is exceeded, regardless of whether this thread has completed execution will be recycled, and then the main thread or function will be executed.

123456789101112131415161718192021222324 importthreadingimporttime classMyThread(threading.Thread):    def__init__(selfid):        threading.Thread.__init__(self)        self.id=id    defrun(self):        =0        time.sleep(10)        print(self.id)        print(‘线程结束:‘+str(time.time()))if __name__ =="__main__":    t1 =MyThread(999)    print(‘线程开始:‘+str(time.time()))    t1.start()    print(‘主线程打印开始:‘+str(time.time()))    forinrange(5):        print(i)    time.sleep(2)    print(‘主线程打印结束:‘+str(time.time()))

Thread Start: 1497534590.2784667
Main thread print start: 1497534590.2794669
0
1
2
3
4
Main thread print End: 1497534592.279581
999
Thread End: 1497534600.2800388

From the printed results, the thread T1 start, and the main thread does not wait for the threads to execute after the end of the T1 run, but executes the subsequent statements while the threads execute.

Now, add the join () method behind the startup thread (the other code does not change)

12345678910111213141516171819202122232425 importthreadingimporttimeclassMyThread(threading.Thread):    def __init__(selfid):        threading.Thread.__init__(self)        self.id=id     defrun(self):        =0        time.sleep(10)        print(self.id)        print(‘线程结束:‘+str(time.time()))if__name__ == "__main__":    t1 =MyThread(999)    print(‘线程开始:‘+str(time.time()))    t1.start()    t1.join()    print(‘主线程打印开始:‘+str(time.time()))    forinrange(5):        print(i)    time.sleep(2)    print(‘主线程打印结束:‘+str(time.time()))

Thread Start: 1497535176.5019968
999
Thread End: 1497535186.5025687
Main thread print start: 1497535186.5025687
0
1
2
3
4
Main thread print End: 1497535188.5026832

After the thread T1 start, the main thread stops at the join () method, and after the child thread T1 ends, the main thread continues to execute the statement following the join.

2, Setdaemon () method. In the main thread A, sub-thread B is created, and B.setdaemon () is called in the main thread A, which means that the main thread A is set to the daemon thread, and if the main thread a executes, it will not be done by pipe B. and the main thread a exits. This is the meaning of the Setdaemon method, which is essentially the opposite of join. In addition, there is a special note: You must set it before the start () method call.

1234567891011121314151617181920212223242526 importthreadingimporttimeclassMyThread(threading.Thread):    def__init__(selfid):        threading.Thread.__init__(self)        self.id= id    defrun(self):        =0        time.sleep(10)        print(self.id)        print("This is:"+self.getName()) # 获取线程名称        print(‘线程结束:‘+str(time.time()))if__name__ =="__main__":    t1 =MyThread(999)    print(‘线程开始:‘+str(time.time()))    t1.setDaemon(True)    t1.start()    print(‘主线程打印开始:‘+str(time.time()))    forinrange(5):        print(i)    time.sleep(2)    print(‘主线程打印结束:‘+str(time.time())) 

Thread Start: 1497536678.8509264
Main thread print start: 1497536678.8509264
0
1
2
3
4
Main thread print End: 1497536680.8510408

T1.setdaemon (True) to set the child thread in order to daemon the thread. Based on the meaning of the Setdaemon () method, the parent thread finishes printing the content, regardless of whether the child thread has finished executing.

If no T1.setdaemon (True) is added before the thread starts, the output is:

Thread Start: 1497536865.3215919
Main thread print start: 1497536865.3215919
0
1
2
3
4
Main thread print End: 1497536867.3217063
999
This is:thread-1
Thread End: 1497536875.3221638

The program runs, executes a main thread, if the main thread and then create a sub-thread, the main thread and the child thread suited two, respectively, then when the main thread completes to exit, will verify that the child thread is completed, if the child thread is not completed, the main thread will wait for the child thread to complete before exiting;

Sometimes what we need is that the sub-thread runs out before it continues to run the main thread, and then it can use the Join method (after the thread starts);

But sometimes we need to, as long as the main thread is complete, regardless of whether the child thread is complete, and the main thread to exit, then you can use the Setdaemon method (thread start front).

Reference Blog: http://www.cnblogs.com/UncleYong/p/6987112.html

The difference and usage of threading join and Setdaemon in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.