Python---process, thread

Source: Internet
Author: User

First, the process

Second, the thread

1, creating a thread using the thread method

#!/usr/bin/env python#-*-coding:utf-8-*-__author__='Zhoufeng'ImportThreadingImport TimedefF1 (ARG): Time.sleep (5)    Print(ARG) T=threading. Thread (target=f1,args= (123,))#target indicates what to do with the thread, and args is the parameterT.start ()#thread into readiness, waiting for CPU to dispatchPrint('End')" "The output is as follows: End123" "#The main thread executes to the last but the program does not exit, waiting for the subroutine to finish executing
View Code

2, the main thread by default is waiting for the child thread to complete before exiting, can also be set by the Setdaemon method to the main thread does not wait for the child thread

#!/usr/bin/env python#-*-coding:utf-8-*-__author__='Zhoufeng'ImportThreadingImport TimedefF1 (ARG): Time.sleep (5)    Print(ARG) T=threading. Thread (target=f1,args= (123,))#target indicates what to do with the thread, and args is the parameterT.setdaemon (True)#set the main thread to unequal child threadsT.start ()#thread into readiness, waiting for CPU to dispatchPrint('End')" "The output is as follows: End" "#after the main thread executes, it exits.
View Code

3, the previous Setdaemon method can only decide to wait until the sub-thread finishes executing, the more flexible join method can specify where to wait, up to how long to wait (even if the child process is not completed and continue to execute)

#!/usr/bin/env python#-*-coding:utf-8-*-__author__='Zhoufeng'ImportThreadingImport TimedefF1 (ARG): Time.sleep (5)    Print(ARG) T=threading. Thread (target=f1,args= (123,)) T.setdaemon (True) T.start () T.join (2)#indicates that the main thread execution is paused here, waiting for the child process to execute, up to 2 seconds to waitPrint('End')" "The output is as follows: End" "#in this case, you cannot wait for the child process to complete, the child process takes 5 seconds, and the main process waits only 2 seconds#the Join method does not conflict with the Setdaemon method
View Code

Python---process, thread

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.