The role of joins in Python's multi-threading

Source: Internet
Author: User

1 python default parameters after the thread is created, regardless of whether the main thread finishes executing, it waits for the child thread to complete before exiting, with or without a join result
Examples are as follows:

import threadingimport timedef say(name):    print(‘%s is start ‘ % name)    time.sleep(3)    print(‘%s is stop‘%name)print(‘___主线程开始___‘,time.time())t = threading.Thread(target=say,args=(‘eve‘,))t.start()t.join()print(‘___主线程结束___‘,time.time())

import threadingimport timedef say(name):    print(‘%s is start ‘ % name)    time.sleep(3)    print(‘%s is stop‘%name)print(‘___主线程开始___‘,time.time())t = threading.Thread(target=say,args=(‘eve‘,))t.start()#t.join()print(‘___主线程结束___‘,time.time())

2 If a thread is created and daemon is set to true, that is, Thread.setdaemon (true), the main thread exits automatically after execution and does not wait for the result of the child thread to execute. And as the main thread exits, the child thread also dies.
Examples are as follows:

import threadingimport timedef say(name):    print(‘%s is start ‘ % name)    time.sleep(3)    print(‘%s is stop‘%name)print(‘___主线程开始___‘)t = threading.Thread(target=say,args=(‘eve‘,))t.setDaemon(True)t.start()# t.join()print(‘___主线程结束___‘)

3 The Join method is blocking, waiting for the child thread to end, and the join method has a parameter of timeout, that is, if the main thread waits for timeout and the child thread has not ended, the main thread forces the end of the child thread.
Examples are as follows:
①. The main thread blocks until those time is less than the child thread run time, the main thread ends, regardless of whether the child thread ends or not.

import threadingimport timedef say(name):    print(‘%s is start ‘ % name)    time.sleep(3)    print(‘%s is stop‘%name)print(‘___主线程开始___‘)t = threading.Thread(target=say,args=(‘eve‘,))t.setDaemon(True)t.start()t.join(2)print(‘___主线程结束___‘)

②. The main thread blocks until those time is greater than the child thread run time, the main thread waits for the child thread to end.

import threadingimport timedef say(name):    print(‘%s is start ‘ % name)    time.sleep(3)    print(‘%s is stop‘%name)print(‘___主线程开始___‘)t = threading.Thread(target=say,args=(‘eve‘,))t.setDaemon(True)t.start()t.join(4)print(‘___主线程结束___‘)

4 The timeout parameter in join is invalid if the thread daemon property is false. The main thread waits for the child thread to end.

import threadingimport timedef say(name):    print(‘%s is start ‘ % name)    time.sleep(3)    print(‘%s is stop‘%name)print(‘___主线程开始___‘)t = threading.Thread(target=say,args=(‘eve‘,))t.setDaemon(False)t.start()# t.join(4)print(‘___主线程结束___‘)

5 If the thread daemon property is true, the timeout parameter in the join is valid, and the main thread waits for timeout time to end the child thread. There is a pit here, that is, if you have N child thread join (timeout) at the same time, the main thread will actually wait for the maximum timeout of n * timeout, because each child thread's time-out starts at the end of the last child line blocks until those.
Examples are as follows:

import threadingimport timedef say(name):    print(‘%s is start ‘ % name)    time.sleep(3)    print(‘%s is stop‘%name)print(‘___主线程开始___‘)t = threading.Thread(target=say,args=(‘eve‘,))t.setDaemon(True)t.start()t.join(4)print(‘___主线程结束___‘)

The role of joins in Python's multi-threading

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.