Since the python interpreter is single-threaded, is it necessary to implement multi-threaded programming?

Source: Internet
Author: User
No more threads can bypass the interpreter. Is it a single thread? In addition, thread switching consumes. In this case, is it necessary for python to implement multi-threaded programming? No more threads can bypass the interpreter. Is it a single thread? In addition, thread switching consumes. In this case, is it necessary for python to implement multi-threaded programming? Reply content: it is necessary to solve at least many IO blocking problems.

IO blocking may occur in many cases, such as networks, disks, and so on. When a blocking occurs, Python does not consume CPU. At this time, if it is a thread, it will not be able to handle other things. Therefore, for environments that contain IO blocking. Multithreading at least gives you the opportunity to run a CPU core to 100%.

Another use comes from Python's C extension module. GIL can be released in the extension module. However, no Python API should be called during GIL release. Therefore, for some very heavy computing, you can write it as a C Module, release GIL before calculation, re-apply for GIL after calculation, and return the result to Python. In this way, the Python process can use more CPU resources. Every Python thread is an OS-level pthread thread. Using Python to manage these threads is more convenient than operating pthreads at the C level. Multithreading was not initially used to solve the problem of multi-core utilization.

Is used to solve the problem of CPU idle when I/O is occupied.
If you have any questions, review the relevant operating system chapters. Multi-process is recommended. multithreading is not a Python dish. Or asynchronous io. Multithreading can be used to solve blocking problems, and Event Response Mechanisms (or something similar to the signal slot) can be implemented. If the running bottleneck is not in the CPU operation but in the I/O (network, multithreading is obviously cost-effective.

Think thread switching overhead is high? Then coroutine ......
Do you think GIL cannot substantially improve performance? Let's talk about multi-process ......

By the way, you can also build a module in pure C, and implement multiple threads in the module at will. You can just explain it to python later. In the single-core era, the cpu is also single-threaded? But why can't I execute multiple tasks and multiple threads? If your task is multi-threaded, it must be multi-threaded.
For example, for network communication, you can input your own words and listen to the words that others send to you.
Use at least two threads.

As for the tasks computed at the fastest speed, multithreading is meaningless.
Multi-process makes sense. You can use multiple CPU cores for Distributed Computing. For example, if a task is divided into four tasks, you can run computing-intensive multi-process at the same time with four cores, and I/O-intensive python multithreading. Of course you can go to GIL... It is rarely used ..

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.