A little exploration of join () and setDaemon () in python multi-thread programming

Source: Internet
Author: User

For the usage of join () and setDaemon () in python multi-thread programming, I have not understood the online data these two days, so I just want to do an experiment. First, write the basic code of the experiment, create a class named MyThread, and then pass the print_func method to this class, and create two sub-threads respectively: copy the Code #! /Usr/bin/env pythonimport threadingimport time class MyThread (threading. thread): def _ init _ (self, func, args, name = ''): threading. thread. _ init _ (self) self. name = name self. func = func self. args = args def run (self): apply (self. func, self. args) def print_func (num): while True: print "I am thread % d" % num time. sleep (1) threads = [] t1 = MyThread (print_func, (1,), print_func. _ name _) threads. append (t1) t 2 = MyThread (print_func, (2,), print_func. _ name _) threads. append (t2) copy the code. First, try setting setDaemon (). Add the following code after the basic code above: for t in threads: t. setDaemon (True) t. start () print "OK \ n" program output: The while loop in I am thread1 I am thread2 okprint_func () does not continue to run and exits. It can be seen that due to setDaemon (True) set the sub-thread as the daemon thread. After the sub-thread starts, the parent thread continues to execute. After the parent thread executes the last print "OK \ n" statement, it does not wait for the sub-thread, the sub-thread is also terminated. The added code is changed as follows: for t in threads: t. start () t. join () print "OK \ n" at this time, the program will output: I am thread1 I am thread1 I am thread1 I am thread1 I am thread1 I am thread1 I am thread1... In this way, we can see that only the first sub-thread is called, the second sub-thread, and the parent thread are not continuing. Here, I understand that the role of join () is that the parent thread of this subthread will be blocked until the sub-thread completes running ., Unable to run. Here, the parent thread cannot continue to execute the for loop, so the second subthread will not appear. Next, modify the code: copy the code for t in threads: t. start () for t in threads: t. join () print "OK \ n" to copy the code. The program output is: I am thread1 I am thread2 I am thread1 I am thread2 I am thread1 I am thread2 I am thread1... It can be seen that both sub-threads are running at this time. Similarly, print "OK \ n" of the parent thread will not be executed before the two sub-threads are completed. In the previous experiment, the running time of two subthreads is the same. If the time consumed by the two threads is different, what will happen if one subthread completes the execution before the other subthread, so I added pass_func: copy the Code #! /Usr/bin/env python # coding = utf-8import threadingimport time class MyThread (threading. thread): def _ init _ (self, func, args, name = ''): threading. thread. _ init _ (self) self. name = name self. func = func self. args = args def run (self): apply (self. func, self. args) def print_func (num): while True: print "I am thread % d" % num time. sleep (1) def pass_func (): pass threads = [] t1 = MyThread (print_func, (1,), pri Nt_func. _ name _) threads. append (t1) t2 = MyThread (pass_func, (), pass_func. _ name _) threads. append (t2) for t in threads: t. start () for t in threads: t. join () print "OK \ n" copy the code. The execution result is: I am thread1 I am thread1 I am thread1 I am thread1 I am thread1 I am thread1 I am thread1 I am thread1 I am thread1... It can be seen that the completion of a sub-thread does not affect another sub-thread, and the parent thread is still blocked. In some cases, you can also set join () for a subthread to achieve a specific effect. For example, the purpose of the following code snippet is to set up a subthread to receive data separately, another sub-thread is used to send data. When the user inputs "quit", the program exits: copy the code def send (sck): while True: data = raw_input ('>') sck. send (data) if data = "quit": sck. close () break def recieve (sck): while True: data = sck. recv (BUFSIZ) print data, "\ n" threads = [] t1 = threading. thread (target = send, args = (tcpCliSock,) threads. append (t1) t2 = threading. thread (target = recieve, Rgs = (tcpCliSock,) threads. append (t2) for t in threads: t. run start () t1.join () to copy the code and add the join () attribute to the subthread corresponding to t1 and send, the parent thread is blocked until the child thread corresponding to the send method ends. If we add the join () attribute to both the t1 and t2 subthreads, this will enable the send method to receive the "quit" command. After exiting the loop, because the recieve method is still in the loop, the parent thread is still blocked and the result program cannot exit. Only add the join () attribute to t1. When t1 ends, the parent thread continues to execute until the last code is executed and then exits, and t2, this is the purpose of the t1.join () command to exit all threads.

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.