If you write more threads than the interpreter is single-threaded, isn't that equal to a single thread? and thread switching consumption, is it necessary for Python to do multithreaded programming?
Reply content:
It is necessary, at least, to solve many IO blocking problems.
There are many situations that can cause IO blocking, such as networks, disks, and so on. When blocking occurs, Python is not CPU-intensive, and if one thread is unable to handle anything else. So for environments that contain IO blocking. Multithreading to a rare opportunity to get you to run a CPU core to 100%.
Another use comes from Python's C extension module. The Gil can be released in the extension module. But no Python API should be called during the release of the Gil. So, for some very heavy computations, you can write a C module, release the Gil before calculation, reapply the Gil after the calculation, and return the results to Python. This allows the python process to take advantage of more CPU resources. Each Python thread is an OS-level pthread thread. Using Python to manage these lines turndown is more convenient to operate pthread at the C level. Multithreading is not the first to solve multi-core utilization problems.
is used to solve the CPU idle problem when IO is occupied.
If you have questions about this, please review the relevant sections of the operating system. Recommended multi-process, multithreading is not a python dish. or asynchronous IO. Multithreading can be used to solve blocking problems, you can do the event response mechanism (or similar to the signal slot), if the operation bottleneck is not CPU operation but on the IO (network), multithreading is obviously a good deal.
Think thread switching overhead is great? That's the process.
Think Gil can't substantially improve performance? That's a lot of progress.
By the way, you can also pure C building modules, modules within the multi-threading, back to Python can be explained on the line. So the single-core CPU is a single thread? But why can't I perform multiple tasks on multiple threads? If your task is multi-threaded, then you have to multithreading
For example, network communication, you lose your words and listen to others.
Then use at least two threads
As for tasks that are calculated at the fastest speed, multithreading doesn't make sense
Multi-process makes sense, can use multi-core CPU to do distributed computing, such as a task cut into 4 tasks, at the same time 4 cores run compute-intensive multi-process, io-intensive with Python multithreading. Of course you're good enough to go to Gil also can ... It's basically a little used to the feeling.