Python Multi-Threading implementation

Source: Internet
Author: User

Introductory case

1 Import Threading,time2 " "3#线程的创建有两种方式,1. Call directly,2. Inheritance4 " "5 # def run (n):6# Print ('Test', N)7# #1. Call Directly8# t1 = Threading. Thread (target=run,args= (1,))9# t2 = Threading. Thread (target=run,args= (2,))Ten # T1.start () One # T2.start () A# #2. Inheritance -#classMythread (Threading. Thread): - # def __init__ (self,n): the # Super (mythread,self). __init__ () -# SELF.N =N - # def run (self): -# Print ('Test', SELF.N) +# t1 = Mythread (1) -# t2 = Mythread (2) + # T1.start () A # T2.start () at #统计多个线程耗时, taking into account the concurrent execution of the thread, each thread points to its own, the main course will end automatically after execution, and the time statistic idea of concurrent execution is to use the Jion () method after the threads execution, similar to the Java Wait () - def run (n): -Print'thread-', N)
#为了更加直观的看到多线程执行的效果, we use the Sleep method
-Time.sleep (2) -Start_time =time.time () -T_list = [] in forIinchRange -): -t = Threading. Thread (target=run,args= ('{}'. Format (str (i)) ) to T.start () + t_list.append (t) - forJincht_list: the J.join () *End_time =time.time () $Print'Cost Time =', End_time-start_time)

Python Multi-Threading implementation

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.