Thread
1 #From threading Import Thread2 #Import Time3 #Import OS4 #def func (i):5 #time.sleep (1)6 #print (' HelloWorld ', I,os.getpid ())7 #thread_lst = []8 #For I in Range (Ten):9 #t = Thread (target=func,args= (i,))Ten #T.start () One #thread_lst.append (t) A #[T.join () for T in Thread_lst] - #print (Os.getpid ())Define your own thread class
1 #From threading Import Thread2 #Import Time,os3 #n = 04 #class MyThread (Thread):5 ## n = 06 ## def __init__ (self):7 ## MYTHREAD.N + = 18 #def run (self):9 #time.sleep (1)Ten #print (' HelloWorld ', N,os.getpid ()) One # A #L = [] - # - #def func (i,l): the #l.append (i) - # - #For i in range: - #t = MyThread () + #T.start ()Waiting for the open thread to finish executing
1 #From threading Import Thread2 #Import Time,os,random3 #4 #def func (i):5 #Time.sleep (Random.random ())6 #print (' Hello%s '%i,os.getpid ())7 #8 #TL = []9 #For I in Range (Ten):Ten #t = Thread (target=func,args= (i,)) One #T.start () A #tl.append (t) - # - #[T.join () for T in TL]There are some attributes of the section thread
1 #From threading Import Thread2 #Import Time,os,random3 #4 #class MyThread (Thread):5 #count = 06 #def __init__ (self,arg1,arg2):7 #super (). __init__ ()8 #self.arg1 = arg19 #self.arg2 = arg2Ten #def run (self): One #Mythread.count + = 1 A #Time.sleep (Random.random ()) - #print ('%s,%s,%s,%s '% (Self.arg1,self.arg2,self.name,os.getpid ())) - #For I in Range (Ten): the #t = MyThread (i,i* ' * ') - #T.start () - #print (T.count) # Calculates the number of open threads
1 fromThreadingImportThread2 ImportThreading3 ImportTime,os,random4 deffunc (i):5 #print (Len (threading.enumerate ()))6 Time.sleep (Random.random ())7 Print(I,threading.current_thread (). Name,threading.current_thread (). Ident)8 forIinchRange (10):9t = Threading. Thread (target=func,args=(i,))Ten T.start () One #print (' main: ', Len (Threading.enumerate ())) # # Returns the list of running threads A Time.sleep (Random.random ()) - #print (' main: ', Len (Threading.enumerate ())) - Print(Threading.active_count ())
The path to Python-multithreading