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 executingView 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 methodView Code
Python---process, thread