11.python Concurrency Primer (part1 process and thread, concurrency, parallel, synchronous, asynchronous)

Source: Internet
Author: User

First, what is the process?

Before you say what a process is, you need to first insert the concept of a process switch!

Process, which can be understood as a running program.

Now consider a scenario where, if there are two programs A and B, program A will need to read a large amount of data input (I/O operations) during the execution of the process, while the CPU can only wait silently until task a reads the data to continue execution, wasting CPU resources. Have you ever thought that while program a reads the data, let program B go, and when program a reads the data, let program B pause. That's fine, of course, but here's a keyword: toggle.

Since it is switching, then this involves state saving, state recovery, plus program A and program B required system resources (memory, hard disk, keyboard, etc.) are not the same. Naturally need to have a thing to record program A and program b what resources are required, how to identify the program A and program B and so on, since it is switching, then this involves the state of the Save, state recovery, plus program A and program B required system resources (memory, hard disk, keyboard, etc.) is not the same. Naturally need to have a thing to record program A and program b what resources are required, how to identify the program A and program B and so on, since it is switching, then this involves the state of the Save, state recovery, plus program A and program B required system resources (memory, hard disk, keyboard, etc.) is not the same. There is a natural need to have something to record what the program A and program B need, how to identify program A and program B, etc.


Simply say the concept of process switching, and then let's talk about the definition of the process.

Process, generally consists of three parts, namely, the program, the data set (the data in this program), Process control block three components,

The program we write is used to describe what the process is going to accomplish and how it is done, and the data set is the resource that the program needs to use in its execution, and the process control block is used to document the external characteristics of the process, to describe the process of its execution, and it can be used to control and manage the process, which is the only sign of

Second, what is a thread?

1. Introduction of Multithreading

If, an editor program, need to receive keyboard input, also need to output the content to the screen, also need to write the content to the hard disk, if only a single process, it will cause only one thing at the same time, (for example, when the text saved to the hard disk, have not been saved before you can do other things).

Next can try to test drive multiple threads, for each function of this program to open a thread, process A to receive the input of the user's keyboard, process B displays the user input content on the screen, process C is responsible for the text data saved to the hard disk.

Here process A,b,c collaboration involves the process of communication problems, and there is a common need to have something, this thing is the content of text!! (This is the concept of the data set in the process mentioned earlier!) )

If these processes are to work together, you must save the data resources to each process! , there are multiple processes, the original data set will be more than a copy!


In this it is necessary to introduce a concept, that is, the data sets between the different processes are independent! The default is not shared!

These processes are constantly switching, resulting in a loss of performance, because the data resources between processes and processes are independent!!


In this case, multithreading is recommended.

Also take the text editor of the new program to give an example, if there is a mechanism, you can make task A, task B, task C to share a data set, so that in the context of the CPU switch, the need to save and restore the content will be much less! And can reduce the loss of communication between the processes! This mechanism is multi-threaded!!


We can understand multithreading as a lightweight multi-process.

A thread is the smallest execution unit of the CPU and is the smallest unit that the program executes.


Threads are composed of "thread ID", "program Counter", "collection", "stack".

The introduction of threads reduces the overhead of program concurrency and improves the concurrency performance of the operating system.


But it's important to note that!! The thread does not have its own data set!!


Third, the relationship between process and thread.

1. Threads can be understood as entities of a process.

2. At least one thread must be included in a process.

3. A thread can only be assigned to a process, and a process may have multiple threads. (We can also understand that a process is a container for threads.) )

4.CPU when allocating resources (datasets), you can only assign resources to processes!!!

5. The thread is actually running on the CPU.


Iv. the concept of parallelism and concurrency.

Parallel processing (Parallel processing): Parallel processing is a computational method by which a computer can perform multiple processing at the same time.

(The main purpose of parallel processing is to save time on large and complex problems)


Concurrent processing (concurrency processing): Refers to a time period in which several programs are running until the run is complete, and the programs are running on the same CPU, but only one program runs on the CPU at any point.


The key to concurrency is that you have the ability to handle multiple tasks, not necessarily at the same time. The key to parallelism is that you have the ability to handle multiple tasks at the same time. So, parallelism is a subset of concurrency.


650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/877318/201705/877318-20170503122648148-1238009778. PNG "width=" 422 "height=" 356 "/>


V. Synchronous vs. asynchronous

Synchronization means that when a process executes a request, if it takes a while for the request to return the information, the process waits until the return message is received, and the process does not need to wait all the time, but continues to perform the following operations, regardless of the state of the other process. The system notifies the process when a message is returned, which can improve the efficiency of execution. For example, the telephone is synchronous communication, the short interest is asynchronous communication.


Vi. Supplementary instructions.

    1. What happens when a process switch is triggered?

      1.1 I/O operations occur

      1.2 This process runs longer than the time slice the CPU allocates to it.

    2. Processes and threads.

      2.1 Process: Resource Management Unit (a container for threads).

      2.2 Thread: the smallest execution unit.




This article is from the "Rebirth" blog, make sure to keep this source http://suhaozhi.blog.51cto.com/7272298/1923953

11.python Concurrency Primer (part1 process and thread, concurrency, parallel, synchronous, asynchronous)

Related Article

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.