Python multi-thread threading module

Source: Internet
Author: User

Use Threading. Thread class, there are three ways to create threads:

    1. Create a thread class and pass it a function;

    2. Create a thread class and pass it to a callable class object;

    3. Derive a class from thread and create an instance of this subclass.




#  Method 1 and Method 2 are created similar to Import threadingdef func (k):     print (' thread %s replies %s '% (Threading.currentthread (). GetName (),  k**2) ) if __name__ ==  ' __main__ ':    mythreads = []     for i in range (1,5):        t =  Threading. Thread (target=func, args= (i,))         mythreads.append (t)      print (' Start: ')     for t in mythreads:         t.start ()      #所以线程创建完后一起start instead of creating one that enables a      for t in mythreads:        t.join      #等待所有线程运行完毕在继续执行主程序 
# Method 3 was created by import Threadingclass Myclass (threading.    Thread): def run (self): print (' I am Thread:%s '% (Self.getname ())) if __name__ = = ' __main__ ': Mythreads = [] For I in range (1,5): T = Myclass () mythreads.append (t) print (' Start: ') for T in Mythreads:t. Start () for T in Mythreads:t.join

The locking operation is not described here.

Python multi-thread threading module

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.