Detailed cascading of multi-core CPU shared data in Python

One: Science one minute

Although processes exist independently and cannot access each other's data, there are inter-process communication methods in Python to help us share data with multi-core CPUs.

For multi-threading, there are some shortcomings. In any scenario, we use multiple threads to complete concurrent processing tasks. Because the CPU operates threads, there are more threads. The resource consumption of the computer is very serious. Multi-threading is suitable for IO operations. The task, then what to do, the emergence of the coroutine helped us solve this problem, the coroutine is a unit smaller than the thread, but its role can not be ignored.

Two: multiple processes

1. Multi-process simple understanding:

The processes are independent and are maintained and managed by the operating system itself. Python starts a process through the C interface, and multiple processes can fully utilize the multi-core CPU.

2. Multi-process creation:

3. Communication between multiple processes:

Processes are independent. If you want to access each other, you must have an intermediate translation. Here are a few ways to communicate between processes.

Process Queue

Analysis:

The Queue communication is equivalent to the parent process assigning a Queue to the child process. After the child process puts the data in the Queue, the child process serializes an intermediate translation and then returns it to the parent process in deserialization.

Because the memory between processes is independent, the object that can't be passed is actually serialized data.

Pipe

Multi-process also has a data transfer method called pipe principle and the same Queue

4. Process lock

Although the memory is independent, even printing will cause print data errors. In order to prevent the screen from being printed out between processes, a process lock is added.

5. Process pool

Creating a child process is equivalent to copying a parent process memory data. In order to prevent frequent creation, resulting in insufficient memory, there is a process pool as a limitation.

Three: Coroutine

1. Simple understanding of the coroutine:

Coroutine, also known as microthreading, coroutne, coroutine is a lightweight thread of user mode. The popular point is that I am resting at home on the weekend. If I wash it first, then cook it, then download the movie and it will be very slow. The effect of using the coroutine is like, when I download the movie, I will ignite the cooking. At this time, I am Wash it right away, wait for me to wash, and the meal is good. After dinner, the movie is good, I can watch it.

2. Creation and use of coroutines:

Gevent is a three-party library that makes it easy to implement concurrent or asynchronous programming via gevent.

Parsing: Try to run the discovery, the running time is the longest time of Sleep, which means that the coroutine can bypass IO and execute, which greatly improves the efficiency.

IO (reading a piece of data from the hard disk, reading data from the network, reading a piece of data from the memory) The operation does not occupy the CPU, the calculation occupies the CPU

3. The coroutine simply crawls the webpage:

4. The coroutine implements socketServer:

Through the coroutine, we can write a socketServer. The bottom layer of the real socketServer is implemented by multi-threading. We use it to write high efficiency and save memory.

Four: Summary

Advantages of the coroutine:

Threads switch between single threads, reducing resource consumption

Simplify the programming model without atomic operational control flow

High concurrency, high expansion, low cost.

Whether it is multi-process, multi-threaded or coroutine with different models in different scenarios to complete the task efficiently.

Vape Pod 4000 Puffs

Axiswell is the world's leading manufacturer of Vape Pens. Our business covers ENDS (Electronic Nicotine Delivery Systems) and other vaping devices, such as disposable vape, refillable vape pen, pod system, CBD vape pen, cbd pods, etc. We maintain factory price, good quality, providing vapers safe and competitive price vaping products.


Moreover, we're dedicated to establishing a healthy and sustainable environment for the whole vaping industry.

Disposable E Cig 4000 Puffs,E Cigarette Disposable,E-Cigarette Vapes Uk,E Cigarette Wholesale Price,Vape Pod Kits

Shenzhen Axiswell Technology Co., Ltd , https://www.medhealthycare.com