Thread
There are two ways to create a thread from the threading module, as follows:
Mode 1:
# !/usr/bin/env python # Coding=utf-8 Import Threading def F1 (ARG): Print (ARG) if __name__ ' __main__ ' : = Threading. Thread (TARGET=F1, args= (1,)) t1.start ()# output # 1
Method 2:
class MyThread (Threading. Thread): def__init__(self, Func, args): = func = args Super (MyThread, self). __init__ () def Run (self): self.func (Self.args)def F2 (ARG): Print = MyThread (F2, 123) Obj.start ()
Output
# 123
Setdaemon method
The main thread is not equal to the end of the thread execution.
#!/usr/bin/env python#Coding=utf-8ImportThreadingImport TimedefF1 (ARG): Time.sleep (2) Print('Start', Arg)if __name__=='__main__': T1= Threading. Thread (TARGET=F1, args= (1,)) T1.setdaemon (True) T1.start ()Print(123)
Output
# 123 instead of 1 12
Join method
Indicates that the main thread waits for the child thread to finish executing, and the join parameter represents the maximum number of seconds
#!/usr/bin/env python#Coding=utf-8ImportThreadingImport TimedefF1 (ARG): Time.sleep (2) Print('Start', Arg)if __name__=='__main__': T1= Threading. Thread (TARGET=F1, args= (1,)) T1.setdaemon (True) T1.start () T1.join ()Print(123)
Output
# 1 123
threads, processes, co-routines