The path to Python-multithreading

Source: Internet
Author: User

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

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.