Some understanding of concurrent programming

Process: A resource collection unit, which is an abstract concept. Process refers to the execution process of the program. 
Thread: Each process has its own thread. Thread is the actual execution unit of the code in the process. The process can be regarded as a workshop. There are
all necessary materials, tools and pipelines for workshop work, so the pipeline actually produces products in this workshop, which is equivalent to the
specific function method code of the CPU in the execution process. From this example, we know that the resources of the workshop can be shared between threads. , and the opening of threads can be very fast, because there is
no need to apply for memory space to the operating system.

Blocking: Waiting for a valid input or I/O to complete, waiting for the resources required for the operation to be in place
Ready: Waiting for the CPU authority, all the resources required for the operation are in place, and the operation can be run immediately after the CPU authority is obtained
: Obtain the CPU authority, use the obtained Resources are being calculated

Multi-threading and multi-process are based on multi-channel technology:
Multi-channel technology:
  spatial multiplexing: realizing physical isolation of programs in memory
  Time multiplexing: rotation of cpu time slices, encountering IO or running When the time reaches a certain time, hand over the CPU running authority

Multi-process: each process exists independently, and the modification of in-process data is limited to the in-process
Advantages: can use multiple cores to realize parallel operations, suitable for cpu-intensive programs
Disadvantages : The process startup overhead is large, and the switching overhead is large
Multi-threading: Each process can only have one thread running at the same time, because of the existence of GIL, parallelism cannot be achieved, but concurrency can be achieved in IO-intensive programs. 
Advantages: The overhead of creating threads is extremely small, and the switching speed is fast. Compared with multi-process It has more advantages in IO-intensive, and can achieve resource sharing.
Disadvantages: only one thread can run at a time, and it cannot take advantage of the multi-core advantage of the computer.
Process pool: The more processes the better, once the number of processes increases, the system will waste more time on the process switching 
overhead switching overhead time is greater than the time saved by multi-process execution, so many processes It is not necessary, so the number of concurrent
processes is limited. After the process pool is established, the operating system will immediately create a corresponding number of processes. After the process start request is issued, all
process requests will be sent by a random process in the process pool that has already been created. Response execution, if the process pool is full, subsequent processes cannot
respond , and must wait until the running process ends, and there are idle processes in the process pool that can be reused, and new process requests can be opened.
Producer and consumer model: 
coupled producers and consumers:
each produces and consumes according to its own capabilities, communicates through blocking queues, and balances the processing capabilities of producers and consumers

GIL
  GIL is the global lock of the CPython interpreter, As the name suggests, GIL is a lock on the interpreter. When each process starts, when the CPython interpreter is called for execution,
a GIL global lock will be generated in the process. Threads compete for the GIL. Obtaining the GIL is equivalent to obtaining the execution authority of the CPython interpreter.
You can call the interpreter to execute code. Because a process has only one GIL of the Cpyton interpreter, in a multi-core computer, even if other threads are allocated CPU execution permission,
but because the thread does not have the execution permission of the interpreter, even if the CPU can be used for operations, the thread code cannot be executed. Therefore, it must wait until the thread that got the GIL releases the GIL. Then all
the threads compete for the GIL and compete for the execution authority of the CPython interpreter. The cycle repeats, and there is always only one thread under a process that can obtain the execution authority of the interpreter.
  That is to say: at any time, there is only one thread in a process. Of course, because the CPU switches and executes very fast in each thread, we can see that almost
all threads are running at the same time, achieving multi-thread concurrency. Effect
  Of course, as in multi-process, the operating system will check the activity status of each thread. Once a thread enters an I/O operation or occupies a CPU time slice for a certain length, the operating system will forcibly 
remove the CPU privilege, because the thread has no CPU resources. executable code, the CPython interpreter immediately asks it to hand over the GIL, frees the GIL, and then all threads compete for the GIL together again.

Guess you like

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