Multi-Threading vs. CPU:
1. Single-core CPU CPU-intensive program (the program to do the calculation) single-threaded (this time the task has already consumed CPU resources 100%, there is no need to use multithreading to improve computational efficiency)
2. Single core CPU IO-intensive program (IO operation program) multithreading > Single threaded (multithreading can be blocked, but not parallel, is "pseudo-parallel", in fact, a CPU is doing everything, just switch too fast, can not detect)
3. Multi-core CPU to do the computational operation of the program multithreading >> single threaded (each core executes a thread, each core thread executes the calculation concurrently, to improve the efficiency of task execution, such as encryption and decryption, data compression decompression (video, audio, normal data), otherwise only a core full, While other cores are idle. )
4. Multi-core CPU IO-intensive tasks multithreading > Single thread
But in Python:
Because the Gil mechanism is not exactly the same: Single core CPU CPU-intensive program single thread time-consuming < multithreading. Multi-core CPU CPU-intensive programs are single-threaded < multi-threaded, meaning that only CPU-intensive programs do not use multithreading alone.
First, say why there will be Gil, Gil is what:
The simplest way of data integrity and state synchronization between multiple threads is to lock it up, so the Python interpreter has a super lock with Gil, and the default Python internal object is thread-safe, eliminating the need to consider additional memory locks and synchronization operations when implemented.
This means that if the lock is not released, the threads are serial.
But Python's multithreading is not useless.
Second, the Gil lock release mechanism:
IO-intensive:
When a thread encounters I/O blocking, the Gil is freed automatically. (When blocking the wait, release the Gil and give another thread the chance to execute)
CPU Intensive:
The interpreter periodically releases the lock to the thread
From the above, there are at least two situations in which Python will switch threads, one is one, but with IO operations, it switches threads, and the other is thread switching when a thread executes a certain number of instructions consecutively.
Plus, each time the operating system execution thread is scheduled, it can be understood why it is much more time-consuming than a single thread to use multiple threads in a multi-core +cpu-intensive program.
Even multi-core CPUs, if there is no Gil different cores of the CPU to execute different threads, but with the Gil of this lock, a core of the CPU even woke up and did not get the Gil Lock, cannot execute.
In general, Python multithreading is best used only for IO-intensive operations without the use of other libraries.
Ref: 5928121
http://cenalulu.github.io/python/gil-in-python/
Multi-threading with CPU and multithreading with Gil