Python concurrent programming multi-process theory part

- what is a process

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

    Example (single core + multi-channel, to achieve concurrent execution of multiple processes):

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

    But egon can only do one task at the same time (CPU can only do one job at the same time), how can we play the effect of concurrent execution of multiple tasks?

    egon prepares a class for a while, then goes to chat with Li Jie's girlfriend, and then goes to play the King of Glory for a while.... This ensures that each task is in progress.

The difference between two processes and programs

A program is just a bunch of code, and a process refers to the running process of a program.

Example:

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

He has a recipe for a birthday cake,

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

In this parable:

    A recipe for a cake is a program (i.e. an algorithm described in an appropriate form)

    A computer scientist is a processor (cpu)

    The various ingredients for making cakes are input data .

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

 

Now suppose alex, the son of computer scientist egon, runs in crying and says: XXXXXXXXXXXXXX .

The scientist Egon thought for a while, the task of dealing with his son Alex's sting was more important than the task of making a cake for his daughter Yuan Hao, so

The computer scientist records where he follows the recipe (save the current state of the process), then pulls out a first aid manual and follows the instructions in it to deal with the sting. Here, we see the processor switch from one process (making the cake) to another high-priority process (giving medical care), each with its own program (recipes and first aid manuals). When the bee sting was dealt with, the computer scientist went back to making the cake,
continuing where he left off.

It should be emphasized that the same program is executed twice, that is also two processes, such as opening Baofengyingyin, although they are all the same software, but one can play Naruto and the other can play Pirates

Three concurrency and parallelism

    Whether it is parallel or concurrent, in the eyes of the user, it is running 'simultaneously'. Whether it is a process or a thread, it is just a task. It is the CPU that really does the work. The CPU does these tasks, and a CPU can only perform a task

A concurrency: It is pseudo-parallel, that is, it appears to be running at the same time. A single cpu + multi-channel technology can achieve concurrency, (parallel also belongs to concurrency)

Two parallel: run at the same time, only with multiple CPUs can parallelism be achieved

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

         There are four cores and six tasks, so four tasks are executed at the same time, assuming 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 will get 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 (it needs to know the scheduling of the process, which cpu is allocated to run, and the operating system has the final say), which may be allocated to any one of the four cpus. implement

 

All modern computers often do a lot of things at the same time, and a user's PC (whether single cpu or multi-cpu) can run multiple tasks at the same time (a task can be understood as a process).

    Start a process to antivirus (360 software)

    Start a process to watch a movie (Baofengyingyin)

    Start a process to chat (Tencent QQ)

All of these processes need to be managed, so a multiprogramming system that supports multiple processes is crucial

Review of the concept of multi-channel technology: multiple (multiple) programs are stored in the memory at the same time, and the cpu is quickly switched from one process to another, so that each process runs for tens or hundreds of milliseconds. In this way, although at a certain moment, A cpu can only perform one task, but in 1 second, the cpu can run multiple processes, which gives the illusion of parallelism, that is, pseudo-concurrency, in order to distinguish the true hardware parallelism of multi-processor operating systems ( Multiple CPUs share the same physical memory)

Four synchronous \ asynchronous and blocking \ non-blocking (emphasis)

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, in fact, most functions are called synchronously. But in general, when we say synchronous and asynchronous, we specifically refer to those tasks that require the cooperation of other components or that take a certain amount of time to complete.

#Example : # 1. apply under multiprocessing.Pool #After initiating a synchronous call, just wait for the task to end in place, regardless of whether the task is computing or blocking in io, in short, just wait for the task to end 
# 2. concurrent. futures.ProcessPoolExecutor().submit(func,).result() 
# 3. concurrent.futures.ThreadPoolExecutor().submit(func,).result()

asynchronous

#The concept of asynchronous is relative to synchronization. When an asynchronous function call is made, the caller cannot get the result immediately. When the asynchronous function completes, the caller is notified via a status, notification, or callback. If the asynchronous function is notified by the state, the caller needs to check it every certain time, and the efficiency is very low (some people who are beginners in multi-threaded programming always like to use a loop to check the value of a variable, which is actually a a serious error). If you use notifications, it is very efficient, because asynchronous functions rarely need to do extra operations. As for the callback function, it is not much different from the notification. #Example : 
# 1. multiprocessing.Pool().apply_async() #After the asynchronous call is initiated 
, it will not wait for the end of the task to return, on the contrary, it will immediately obtain a temporary result (not the final result, it may be encapsulated an object). 
# 2. concurrent.futures.ProcessPoolExecutor(3).submit(func,) 
# 3. concurrent.futures.ThreadPoolExecutor(3).submit(func,)

block

#Blocking call means that the current thread will be suspended (such as io operation) before the call result returns. The function will activate the blocked thread only after the result is obtained. 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 active, but logically the current function does not return. 
#Example : 
# 1. Synchronous call: apply a task that has accumulated 100 million times, the call will wait until the task returns the result, but it does not block (even if the execution permission of the cpu is taken away, it is still ready state); 
# 2. Blocking call: When the socket is working in blocking mode, if the recv function is called without data, the current thread will be suspended until there is data.

non-blocking

#The concept of non-blocking and blocking corresponds to that it will return immediately before the result cannot be obtained immediately, and the function will not block the current thread.

summary:

1. Synchronization and asynchrony are aimed at how functions/ tasks are called: 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 active. In the asynchronous case, when a process initiates a function (task) call, it will not wait for the function to return, but will continue to execute.

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

Creation of five processes (understanding)

But all hardware needs to be managed by an operating system. As long as there is an operating system, there is the concept of a process and a way to create a process. Some operating systems are only designed for one application, such as the controller in a microwave oven. Once started Microwave oven, all processes are already there.

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

  1. System initialization (use the ps command to view the process in linux, use the 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, and the process that runs in the background and only wakes up when needed is called daemons such as email, web pages, news, print)

  2. A process starts a subprocess during the running process (such as nginx opens multiple processes, os.fork, subprocess.Popen, etc.)

  3. The user's interactive request, and create a new process (such as the user double-click Baofengyingyin)

  4. Initialization of a batch job (applies only to mainframe batch systems)

  

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

  1. In UNIX the system call is: fork, which creates an exact copy of the parent process with the same storage image, the same environment strings, and the same open files (in the shell interpreter process, execute A single command will create a child process)

  2. In windows, the system call is: CreateProcess, CreateProcess not only handles the creation of the process, but also is responsible for loading the correct program into the new process.

 

  About created child processes, UNIX and Windows

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

  2. The difference is: in UNIX, the initial address space of the child process is a copy of the parent process, prompt: 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.

The End of Six Processes (Understanding)

1. Normal exit (voluntary, for example, the user clicks the cross on the interactive page, or the program is executed and invokes a system call to initiate a normal exit, use exit in linux, and use ExitProcess in windows)

  2. Exit on error (voluntary, a.py does not exist in python a.py)

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

  4. Killed by another process (involuntary, like kill -9)

Hierarchy of Seven Processes

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

  1. All processes in UNIX are rooted in the init process and 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 keyboard-related process group.

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

Status of eight processes

tail -f access.log |grep '404'

  Execute the program tail, start a child process, execute the program grep, start another child process, communicate between the two processes based on the pipe '|', and use the result of tail as the input of grep.

  The state of the process grep waiting for input (i.e. I/O) is called blocking, and the grep command cannot run at this time

  In fact, in two cases, a process will logically fail to run,

  1. The hang of the process is its own reason. When I/O is blocked, the CPU must be given up for other processes to execute, so as to ensure that the CPU is always working

  2. It has nothing to do with the process. It is the operating system level. It may call other processes to use the CPU due to a process taking too much time or priority.

  Thus a process consists of three states

Implementation of nine-process concurrency

The realization of process concurrency lies in that the hardware interrupts a running process and saves all the running states of the process at this time. To this end, the operating system maintains a table, the process table, and each process occupies a process table. entries (these entries are also called process control blocks)

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324782933&siteId=291194637