Multiple processes of concurrent Python programming (theory)

table of Contents:

  • 1. What is a process
  • Second, the difference between process and program
  • Three, concurrency and parallelism
  • Fourth, synchronous \ asynchronous and blocking \ non-blocking (emphasis)
  • Five, the creation of the process (understand)
  • Six, the termination of the process (understand)
  • Seven, the process hierarchy
  • Eight, the state of the process
  • Nine, the realization of process concurrency (understand)

1. What is a process 

Process: An ongoing process or task. The CPU is responsible for executing the task.

Examples (single core + multiple channels to achieve concurrent execution of multiple processes):

egon has many tasks to do in a time period: python lesson preparation task, book writing task, girlfriend making task, king glory task,  

But egon can only do one task at a time (cpu can only do one job at a time), how can we play the effect of multiple tasks executing concurrently?

egon prepares a lesson, then chats with Li Jie ’s girlfriend, and then plays King Glory again ... this ensures that every mission is in progress.

 

Multi-channel technical illustration:

Multi-channel technology key knowledge

Taking in space and taking in time

  * Spatial multiplexing

    Multiple programs share a set of computer hardware

  * Multiplexing in time

    Example: Washing clothes for 30s, cooking for 50s, boiling water for 30s

      Single channel needs 110s, multiple channels only need the one with longer task ===> Switch to save time

    Example: Playing a game while eating ====> Save state

      Switch + save state

 

Three state diagram of the process running:

 

 

 

Second, the difference between process and program:

The program is just a pile of code, and the process refers to the running process of the program.

Examples:

Imagine egon, a computer scientist with good cooking skills, is baking a birthday cake for his daughter Yuan Hao.

He has recipes for making birthday cakes,

The kitchen has the necessary ingredients: flour, eggs, leeks, garlic, etc.

In this analogy:

The recipe for making a cake is the program (that is, the algorithm described in an appropriate form)

A computer scientist is a processor (cpu)

The various raw materials for making cakes are input data.

The process is the sum of a series of actions such as the chef reading the recipe, taking various ingredients and baking the cake.

Now suppose alex, the son of computer scientist egon, ran in and cried, saying: XXXXXXXXXXXXXX .

Scientist egon thought about it, the task of dealing with the sting of his son alex was more important than the task of making cake for his daughter Yuan Hao, so

The computer scientist recorded what he did according to the recipe (saving the current state of the process), and then took out a first aid manual and handled the sting according to the instructions. Here, we see the processor switch from one process (make cake) to another high-priority process (implement medical treatment), each process has its own program (recipe and first aid manual). After the bee sting was treated, the computer scientist came back to make cakes and continued from the step when he left.

It should be emphasized that the same program is executed twice, and that is also two processes, such as opening the storm video, although they are all the same software, but one can play War Wolf and one can play for more than a year.

Three, concurrency and parallelism

Whether it is parallel or concurrent, it seems to the user that they are running at the same time. Whether it is a process or a thread, it is just a task. It is really the CPU, the CPU to do these tasks, and a CPU can only do this at the same time. Perform a task

* Concurrency It 
  looks like it can run concurrently when it is running at the same time.

 * Parallel in the 
  true sense of simultaneous execution of 

ps:

 * Parallel is definitely concurrency
 * Single-core computers certainly cannot achieve parallelism, but they can achieve concurrency! ! !

 

1. Concurrency: It is pseudo-parallel, that is, it seems to run simultaneously. A single cpu + multi-channel technology can achieve concurrency, (parallel also belongs to concurrency)

Single CPU, multiple processes, concurrent example 1:

You are a cpu, you talk about three girlfriends at the same time, each can be a love task, you are shared by these three tasks 
To play the effect of concurrent love, 
you should go to the movie with your girlfriend 1 first, watch After a while, I said: No, I want to have a diarrhea, and then ran to eat with my second girlfriend, and after a while, I said: Then, I 
went to the bathroom, and then ran a room with my girlfriend 3

Single CPU, multi-process, concurrent example two:

One afternoon, egon, yuanhao, wupeiqi, alex agreed to go prostitute together, but there is only one prostitute, and there is only one cpu, but they have to do 
four tasks 'simultaneously' Egon for a while, Yuanyuan for a while, wupeiqi for a while, and alex for a while 
egon: It cost 200 yuan, because people live well. 
Yuanhao: 500 yuan for 
wupeiqi: 100 yuan, maybe it ’s not 
alex : No money, why? ? ? Because everyone just married his girlfriend

Second, parallel: run at the same time, only with multiple CPUs can achieve parallel

Under single core, you can use multi-channel technology, multiple cores, each core can also use multi-channel technology ( multi-channel technology is for a single core )

There are four cores and six tasks, so that four tasks are executed at the same time, assuming that they are assigned to cpu1, cpu2, cpu3, ​​cpu4,

Once task 1 encounters I / O, it is forced to interrupt execution. At this time, task 5 gets the time slice of cpu1 to execute. This is the multi-channel technology under single core

And once the I / O of task 1 is over, the operating system will call it again (need to know the scheduling of the process, which cpu is assigned to run, it is up to the operating system ), and may be assigned to any of the four cpu To execute

 

 

 

All modern computers often do many things at the same time. A user's PC (whether it is a single CPU or multiple CPUs) can run multiple tasks at the same time (a task can be understood as a process).

Start a process to kill virus (360 software)

Start a process to watch the movie (Storm Video)

Start a process to chat (Tencent QQ)

All these processes need to be managed, so a multi-program system that supports multiple processes is essential

Review of multi-channel technology concepts: multiple (multiple) programs are stored in the memory at the same time, and the CPU quickly switches from one process to another, so that each process runs individually for tens or hundreds of milliseconds. A CPU can only perform one task, but within 1 second, the CPU can run multiple processes, which gives the illusion of parallelism, that is, pseudo concurrency, to distinguish the true hardware parallelism of multiprocessor operating systems ( Multiple CPUs share the same physical memory)

Fourth, synchronous \ asynchronous and blocking \ non-blocking (emphasis)

Describe how tasks are submitted:

Synchronize:

#The so-called synchronization means that when a function call is issued, the call will not return until the result is obtained. 
According to this definition, most functions are called synchronously. But generally speaking, when we talk about synchronous and asynchronous, we specifically refer to tasks that require other components to cooperate or take a certain time to complete.
(Synchronization is submitted after the task, return to their home to wait for the results of the task, the process of waiting does nothing (dry, etc.), and in the program-level program is stuck) popular words
# Examples: # 1. multiprocessing. Apply under Pool #After initiating the synchronous call, just wait for the task to finish in place, regardless of whether the task is calculating or blocking in io, in short, it is a brain to wait for the task to finish # 2. concurrent.futures.ProcessPoolExecutor (). Submit ( func,). result () # 3. concurrent.futures.ThreadPoolExecutor (). submit (func,). result ()

asynchronous:

# Asynchronous and synchronous relative concept. When an asynchronous function call is issued, the caller cannot get the result immediately. When the asynchronous function is completed, the caller is notified by status, notification or callback. 
If the asynchronous function is notified by status, then the caller needs to check it every certain time, and the efficiency is very low (some people who are new to multi-threaded programming always like to use a loop to check the value of a variable, which is actually a A very serious error).
If you use the notification method, the efficiency is very high, because the asynchronous function hardly needs to do additional operations. As for the callback function, it is not much different from the notification.
Popular talk (after the asynchronous task is submitted, no place to return to wait for the results of the task directly to do other things, as a result there will be a return to the task asynchronous callback mechanism for automatic processing)
# example: # 1. multiprocessing.Pool (). apply_async () #After initiating an asynchronous call, it will not wait for the task to end before returning. Instead, it will immediately obtain a temporary result (not the final result, which may be an encapsulated object). # 2. concurrent.futures.ProcessPoolExecutor (3) .submit (func,) # 3. concurrent.futures.ThreadPoolExecutor (3) .submit (func,)

Describe the running state of the program

Blocking:

# Blocking call means before the call returns, the current thread is suspended (in case of operation io). The function activates the blocked thread only after getting the result. Some people may equate blocking calls with synchronous calls, but in fact they are different. 
For synchronous calls, many times the current thread is still activated, but the current function is not returned logically.
# 例 : # 1. Synchronous call: apply a task with a total of 100 million times. The call will wait until the task returns the result, but it is not blocked (even if the execution permission of the CPU is robbed, it is still in preparation State); # 2. Blocking call: When the socket works in blocking mode, if the recv function is called when there is no data, the current thread will be suspended until there is data.

Non-blocking:

# Non-blocking and blocking concept corresponds finger will return immediately before the result can not be obtained immediately, while the function does not block the current thread.

summary:

# 1. Synchronization and asynchrony refer to the calling method of functions / tasks: synchronization means that when a process initiates a function (task) call, it waits until the function (task) is completed, and the process continues to be in an active state. In the asynchronous case, when a process initiates a function (task) call, it does not wait for the function to return, but continues to execute. When the function returns, the process is notified of the completion of the task by means of status, notification, event, etc. 

# 2. Blocking and non-blocking are for processes or threads: blocking is to suspend the process when the request cannot be satisfied, and non-blocking does not block the current process

Five, the creation of the process (understand)

However, all hardware needs an operating system to manage it. As long as there is an operating system, there is a concept of a process, and there is a way to create a process. Some operating systems are designed for only one application, such as a controller in a microwave oven, once started Microwave ovens, all processes already exist.

For a general-purpose system (running many applications), the ability to create or cancel processes during system operation is required, mainly divided into 4 forms to create new processes

1. System initialization (view process using ps command in linux, task manager in windows, the foreground process is responsible for interacting with the user, the process running in the background has nothing to do with the user, the process running in the background and only awakening when needed, called Daemons, such as email, web pages, news, printing)

2. A process opened a subprocess during the running process (such as nginx open multi process, os.fork, subprocess.Popen, etc.)

3. The user's interactive request to create a new process (such as the user double-clicking Storm Video)

4. Initialization of a batch job (only used in the mainframe batch system)

Either way, the creation of a new process is created by an existing process executing a system call to create the process:

1. In UNIX, the system call is: fork, fork will create an exact copy of the parent process, the two have the same storage image, the same environment string and the same open file (in the shell interpreter process, execute A command will create a child process)

2. The system call in Windows is: CreateProcess, CreateProcess not only handles process creation, but also is responsible for loading the correct program into the new process. 

About the created child process, UNIX and windows

1. The same is: after the process is created, the parent process and the child process have their own different address spaces (multi-channel technology requires physical layer to achieve memory isolation between processes), and any process in the address space will not be modified Affects another process.

2. The difference is that in UNIX, the initial address space of the child process is a copy of the parent process. Note: the child process and the parent process can have a read-only shared memory area. But for the Windows system, the address space of the parent process and the child process is different from the beginning.

Six, the termination of the process

1. Normal exit (voluntary, such as the user clicking the cross on the interactive page, or the program execution is completed, the system call is called to initiate normal exit, use exit in Linux, and ExitProcess in windows)

2. Exit with error (voluntarily, a.py does not exist in python a.py)

3. Serious errors (involuntary, execution of illegal instructions, such as referencing non-existent memory, 1/0, etc., can catch exceptions, try ... except ...)

4. Killed by other processes (involuntary, such as kill -9)

Seven, the process hierarchy

Regardless of UNIX or windows, the process has only one parent process, the difference is:

1. All processes in UNIX use the init process as the root to form a tree structure. The parent and child processes together form a process group, so that when a signal is sent from the keyboard, the signal is sent to all members of the current process group associated with the keyboard.

2. In windows, there is no concept of process hierarchy, all processes are in the same status, the only hint similar to process hierarchy is that when creating a process, the parent process gets a special token (called a handle), which The handle can be used to control the child process, but the parent process has the right to pass the handle to other child processes, so there is no hierarchy.

Eight, the state of the process

tail -f access.log |grep '404'

Execute the program tail, start a sub-process, execute the program grep, start another sub-process, based on the pipeline '|' communication between the two processes, and use the result of tail as the input of grep.

The state of the process grep while waiting for input (that is, I / O) is called blocking. At this time, the grep command cannot run.

In fact, in two cases, a process cannot be run logically,

1. The process is suspended because of its own reasons. When I / O is blocked, the CPU must be let out to allow other processes to execute, so as to ensure that the CPU has been working.

2. It has nothing to do with the process. It is at the operating system level, and may call other processes to use the CPU because of a process taking up too much time or priority.

Thus a process consists of three states

 

 

 Nine, the realization of process concurrency

The realization of process concurrency is that the hardware interrupts a running process and saves all the state of the process at this time. For this reason, the operating system maintains a table, that is, the process table (process table), each process occupies a process table Items (these table items are also called process control blocks)

 

 This table stores important information about the state of the process: program counter, stack pointer, memory allocation status, the status of all open files, account and scheduling information, and other information that must be saved when the process changes from running to ready or blocked Information to ensure that the process starts again as if it had never been interrupted.

 

Guess you like

Origin www.cnblogs.com/baicai37/p/12752696.html