12th article: Threads and Processes (i)

Source: Internet
Author: User
Tags thread class

This article first introduces the concept of multitasking, then introduces the process of creating child threads in the thread class under the threading module, and finally the mutual exclusion lock. Deadlock and how to avoid the deadlock and other knowledge. And the process is described in the next article.

First, the concept

Before we understand the knowledge of multitasking or threading, we need to know some conceptual knowledge first.

  1. Time Slice rotation

  If the terminal is a single-core CPU, and I want to run multiple programs at the same time, usually we can see the single core CPU can also run multiple programs, it is because the operating system to allocate CPU calls, each program takes up CPU time is very short, and then start the next program run, The cycle of running these programs, because each program consumes CPU time is particularly short so the phenomenon is presented to us is: multiple programs running at the same time the illusion.

  2. Concurrency

When the number of CPUs is less than the number of running programs, can not be realized in the real sense of each program running at the same time, it is also the use of time-slice rotation principle to create the illusion that each program runs simultaneously, that is, concurrency.

    

  3. Parallel

When the number of CPUs is greater than the number of running programs, you can implement each program has always occupied a CPU, to achieve a real sense of each program running concurrently, that is, parallel.

  4. Main thread

When we execute a program, there will be a similar pipeline process, in accordance with the execution of the program execution, and in the process of executing the main thread can create sub-threads, and usually the main thread routines and so on all sub-threads run to end the run, if the main thread is dead, all child threads will also be killed.

5. Child threads

A child thread can be created through the main thread, while the child thread is running to end or be killed without affecting the execution of the main thread.

Second, the thread

  1. Why cable? let's start by looking at the example:

#!/usr/bin/env python#-*-coding:utf-8-*-Import TimedefSing ():#singing     forIinchRange (3):        Print("He is singing song.") Time.sleep (1)defDance ():#Dancing     forIinchRange (3):        Print("He is dancing.") Time.sleep (1)defmain (): Sing () dance ( )if __name__=="__main__": Main ()
01-In case of no multi-threading

Note: From the above example can be seen: in the case of no multi-threading, can only sing the song before the dance, but we have to achieve is to sing while dancing

#!/usr/bin/env python#-*-coding:utf-8-*-Import TimeImportThreadingdefSing ():"""singing"""     forIinchRange (3):        Print("He is singing song.") Time.sleep (1)defDance ():"""Dancing"""     forIinchRange (3):        Print("He is dancing.") Time.sleep (1)defMain ():#1. Create a child thread object and specify the working function of the child thread    #that's the equivalent of hiring a company to tell him what he needs to do.T1 = Threading. Thread (target=sing) T2= Threading. Thread (target=dance)#2, the child thread runs, and the work function begins to work    #equivalent to the employee starting workT1.start () T2.start ( )#3, the main thread and other sub-threads run out before closingT1.join () t2.join ( )if __name__=="__main__": Main ()
02-In the case of multiple threads

Note: Through the above examples can be obtained: through the realization of multi-tasking singing and also danced.

  

 2. What is the process of creating threads?

Importtime,threadingdefSing (): forIinchRange (3):        Print("He is singing song.") Time.sleep (1)defDance (): forIinchRange (3):        Print("He is dancing.") Time.sleep (1)defMain ():#1. Create a child thread object and specify the working function of the child thread    #that's the equivalent of hiring a company to tell him what he needs to do.T1 = Threading. Thread (target=sing) T2= Threading. Thread (target=dance)#2, the child thread runs, and the work function begins to work    #equivalent to the employee starting workT1.start () T2.start ( )#3, the main thread and other sub-threads run out before closingT1.join () t2.join ( )#Note: The multi-tasking realizes the singing while also carries on the dance. if __name__=="__main__": Main ()

Personal Understanding:

Note: in fact, the relationship between the main thread and the child thread can be understood as:

1, the main thread is equivalent to the operation of a company, and the sub-thread equivalent to a company under the work of an employee;

2. The process of creating a child thread is t1=threading. Thread (target=func_name,args= (para1,para2.), we can understand to hire a new employee for the company , and Target=func_name, The equivalent of telling the employee what he needs to do the job is to pass in the function function;

3, and T1.start (), it means that the child thread is actually running, function is called, the process can be understood as the employee began to work, that is, the employee's job is a child thread, and the employee itself is only a child thread object.

  

12th article: Threads and Processes (i)

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.