Python basic learning-concurrent programming

1. Background Knowledge

As the name implies, a process is a process being executed. A process is an abstraction of a running program.

The concept of process originates from the operating system, is the core concept of the operating system, and is also one of the oldest and most important abstract concepts provided by the operating system. Everything else in the operating system revolves around the concept of a process. So if you want to really understand the process, you must understand the operating system in advance

Operating system development history: just refer to the blog: https://www.cnblogs.com/Dominic-Ji/articles/10929381.html

The necessary theoretical basis:

 

# 1 The role of the operating system: 
    1 : hide the ugly and complex hardware interface, provide a good abstract interface
     2 : manage and schedule processes, and make the competition of multiple processes for the hardware orderly 

# 二 多 道 技术: 
    1. Generate Background: For single core, to achieve concurrent 
    ps: the 
    current host is generally multi-core, then each core will use multi-channel technology 
    with 4 cpu, a program running on cpu1 encounters io blocking, and will wait until io ends before rescheduling , Will be scheduled to 
    any one of the four CPUs, specifically determined by the operating system scheduling algorithm. 
    
    2. Spatial multiplexing: if there are multiple programs in memory at the same time
     3. Multiplexing in time: multiplexing the time slice of a cpu 
       emphasizes: when encountering io cut, it takes too long to occupy the cpu time, the core is before the cut Save the state of the process, so as 
            to ensure that the next time you switch back, you can continue to run based on the position of the last cut

Second, multi-channel technology

1. Single-core implementation of concurrent effects

  • Concurrency

    It looks like it is running at the same time and can be called concurrent

  • parallel

    Simultaneous execution in the true sense

to sum up:

  • Parallel must be concurrent

  • Single-core computers cannot achieve parallelism, but they can achieve concurrency

Supplement: We directly assume that a single core is a core, and only one person works, don't consider the number of cores in the CPU

2. Multi-channel technical illustration

3. Multi-channel technology focus

  • Spatial multiplexing
    • Multiple programs share a set of computer hardware
  • Multiplexing in time
    • Only need to make the long one, can save the switching time

    • Switch between save state, user mode and kernel mode Switch + save state

 

Switching (CPU) is divided into two cases 1. When a program encounters IO operation, the operating system will deprive the program of the CPU execution authority role: improve the CPU utilization and does not affect the execution efficiency of the program When io is delayed, the program does not have any room to execute the cup, so the deprivation will not affect the operation of the program. 2. When a program occupies the CPU for a long time, the operation attraction will also deprive the program of the CPU execution authority. Execution efficiency (original time + switching time)

3. Process Theory

1. Required knowledge

'' ' 
The difference between a program and a process A 

program is a bunch of code lying on the hard disk, a 
process that is' dead' is a process that is being executed, it is a 'live 
' ''

 

2. Process scheduling

  • First come first served scheduling algorithm
    • It is good for operations with long execution time, but not for short operations.
    • Because a long job arrives first, the next short job has to wait for the long job to be completed before emptying the cup space for the short job
  • Short job priority algorithm
    • Prioritize short jobs
    • If a long job is running and a short job comes, then the long job will be suspended first, and then the short job will be returned to the long job to continue execution
  • Time slice rotation method + multi-level feedback queue
    • No matter long or short jobs are put into a queue first, uniformly execute a specified unit time
    • If it is not completed within the specified time, it will fall into the second priority queue and execute a longer unit time
    • At this time, a job will directly suspend the second echelon to perform the work of the first echelon
    • Summary: Utilize the switching and saving state, optimize the execution efficiency of different size jobs

3. Three states of the process

Before understanding other concepts, we must first understand several states of the process. In the process of running the program, due to the scheduling algorithm of the operating system, the program will enter several states: ready, running and blocking.

  (1) Ready status

   When the process has been allocated to all necessary resources except the CPU, as long as the processor is obtained, it can be executed immediately. The process state at this time is called the ready state.

  (2) Execute / Run

StateWhen the process has acquired the processor, its program is being executed on the processor, and the state of the process at this time is called the execution state.

  (3) Blocked (Blocked) state

The process being executed cannot be executed due to waiting for an event to occur, it will give up the processor and be in a blocked state. There can be multiple events that cause process blocking, for example, waiting for I / O to complete, the application buffer cannot be satisfied, waiting for letters (signals), etc.

4. Two pairs of important concepts

Describes how tasks are submitted

  • Synchronize
    • Explanation: After the task is submitted, it waits for the return result of the task in place, and does not do anything during the waiting process. It looks like the program is stuck.
  • asynchronous
    • Explanation: After the task is submitted, do not wait for the return result of the task, and do other things directly
    • The return result of the task will be automatically processed by an asynchronous callback mechanism

It's like handing over an assignment to the teacher for correction. Xiaohong handed it over to the teacher, standing in the same place and waiting for the teacher to finish the correction. And Xiao Li ran away after giving it to the teacher. This behavior is called asynchronous.

Describes the running state of the program

  • Blocking (blocking state)
    • It is the state that our program will basically encounter when running, waiting for io operation
  • Non-blocking (ready state, running state)
    • It is the ideal state for our program to run, only switching between the ready state and the running state, reducing io delay

Four, two ways to start the process

# 方式一
from multiprocessing import Process
import time


def task(name):
    print(f'{name} is running')
    time.sleep(3)
    print(f'{name} is over')


if __name__ == '__main__':
    p = Process(target=task, args=('hz',))
    p.start()
    print('^(* ̄(oo) ̄)^')
    
# 方式二 类的继承

from multiprocessing import Process
import time

class MyProcess(Process):
    def run(self):
        print('hell 1')
        time.sleep(3)
        print('hello 2')

if __name__ == '__main__':
    p = MyProcess()
    p.start()
    print('^(* ̄(oo) ̄)^')

to sum up

"" " 
Creating a process is to apply for a piece of memory space in the memory and throw the code that needs to be run into. 
A process corresponds to a separate memory space 
in the memory. Multiple processes correspond to a separate block of memory space in the 
process. Process and process By default, inter-data cannot be directly interacted with. If you want to interact, you can use third-party tools and modules 
"" "

join method

Join is to let the main process wait for the sub-process code to finish running before continuing. Does not affect the execution of other child processes

Specific use: After the process starts, the process object .join ()

Data isolation between processes

from multiprocessing import Process
import time

a = 100
def task(name):
    print(f'{name} is running')
    time.sleep(3)
    global a
    a = 101
    print(a)
    print(f'{name} is over')


if __name__ == '__main__':
    p = Process(target=task, args=('hz',))
    p.start()
    print(a)
    p.join()
    print('^(* ̄(oo) ̄)^')

Guess you like

Origin www.cnblogs.com/dingbei/p/12755339.html