How does python handle concurrent programming (practical skills)

Before we tackle concurrent programming let's get to the basics, which is the core of our concurrent programming problems

1. What is a program? What is a process?
  A program is a bunch of files
  , a process is an executing process/program

  So the process is an abstract concept,
  which originates from the operating system

2. What is an operating system?
  Definition: An
    operating system is a control program that is located between computer hardware and application software and is
    used to coordinate, manage, and control the resources of computer hardware and software.

3. The two major functions of the operating system:
  1. Encapsulate complex and ugly hardware operations into beautiful interfaces and provide them for application use
  2. Make the competition between processes and hardware orderly


4. Multi-channel technology
  The background of multi-channel generation is to achieve the effect of concurrent execution of multiple processes in the case of a single CPU

  Spatial multiplexing (multiple programs multiplexing memory space)
  temporal multiplexing (multiple programs multiplexing CPU time) The
  cpu needs to switch when IO operations are encountered (to improve efficiency) if
  a process occupies the CPU for too long, it will also be cut ( Reduce efficiency)

  The memory space between processes is isolated from each other

 

Multiple processes in python concurrent programming

1 What is a process

A process is an ongoing program or task

2 The difference between a process and a program

A program is a bunch of code, or a file, and a process is the running process of a program

It should be noted that qq is a program. I use qq to log in to Ma Huatong's qq on the computer, and at the same time I log in to Ma Huateng's eldest daughter-in-law's qq. These are two processes.

3 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

  Concurrency: A single cpu is pseudo-parallel, that is, it seems to be running at the same time, but it is not, a single cpu + multi-channel technology can achieve concurrency

  For example, you can watch TV while your colleagues eat 

  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)

 

Guess you like

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