Python3 Processes and Threads

Source: Internet
Author: User

Processes and Threads
    1. Process: Memory independent, thread sharing memory of the same process
    2. A process is a collection of resources, a thread is an execution unit
    3. Processes cannot directly access each other, and processes in the same process can communicate with each other
    4. Create a new process consumes system resources, threads are very lightweight, saving only the necessary data when the thread is running, such as context, program stack information
    5. Threads in the same process can control each other, and the parent process can control the child process
1 ImportThreading2 Import Time3 4 defsayhi (num):5     Print('Num:', num)6Time.sleep (3)7 8A = Threading. Thread (target=sayhi,args= (1,))9b = Threading. Thread (target=sayhi,args= (2,))TenNow1 =time.time () One Print(NOW1) A A.start () - B.start () -Now2 =time.time () the Print(NOW2) - Print(Threading.active_count ()) - #includes main thread, total 3 - Print(A.getname ()) + Print(B.getname ()) -  + classMyThread (Threading. Thread): A     def __init__(self,n): atThreading. Thread.__init__(self) -SELF.N =N -  -         defRun (self): -             Print('running on thread $s'%SELF.N) -Time.sleep (3) inNOW3 =time.time () - Print(NOW3) toT1 = MyThread (1) +T2 = MyThread (2) - T1.start () the T2.start () * Print(T1.getname ()) $ Print(T2.getname ())Panax NotoginsengNOW4 =time.time () - Print(NOW4) the  +Thread_list = [] A  forIinchRange (10): theS1 = Threading. Thread (target=sayhi,args=(i,)) + S1.start () - thread_list.append (S1) $NOW5 =time.time () $ Print(NOW5) -  forRinchthread_list: -R.join ()#s1.wait () the Print('--work done--') -NOW6 =time.time ()Wuyi Print(NOW6) the Print('Primary'. Center (20,'-')) -  Wu  -  forIiinchRange (10): AboutS2 = Threading. Thread (target=sayhi) $ S2.setdaemon (S2) -S2.start ()
View Code

Python3 processes and Threads

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.