In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what is the reason why Python multi-thread can not use multi-core". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the reason why Python multi-thread can not use multi-core".
1. Global interpretation lock
Such as the question: why can't multithreading in Python take advantage of multi-core processors?
Global interpreter lock (Global Interpreter Lock) is a mechanism used by computer programming language interpreters to synchronize threads so that only one thread is executing at any time.
Even on multi-core processors, interpreters that use GIL only allow one thread to execute at a time. Common interpreters that use GIL are CPython and Ruby MRI.
You can see that GIL is not a unique feature of Python, but a mechanism for interpreting languages to deal with multithreading problems rather than a language feature.
Interpreter of 2.Python
Python is an interpreter language, and the code is executed by the interpreter. There are many interpreters in Python, which are developed based on different languages, and each interpreter has its own characteristics.
A schematic diagram of the interpretation and execution of the Python program:
CPython
CPython is the mainstream version of the interpreter, this interpreter is written in C language, is also the most widely used interpreter, and can easily interact with the class library of C _ Candle +, so it is also the most concerned interpreter.
Jython
A python interpreter written in java language is an interpreter that compiles python into Java bytecode and then executes it, which can easily interact with Java's class library.
IronPython
Interpreting the Python code as bytecode running on the .net platform for execution, similar to the Jython interpreter, can easily interact with the class libraries on the .net platform. IPython
The interaction has been enhanced, but the execution process and functionality are the same as CPython.
PyPy
A compiler using JIT (just-in-time) technology that focuses on execution speed and dynamically compiles Python code, thereby improving the execution speed of Python.
PyPy in the process of dealing with python code, the processing of a small number of functions and CPython implementation results are different, if the project to use PyPy to improve execution efficiency, be sure to know the difference between PyPy and CPython in advance.
3.CPython 's thread is not safe
The thread of CPython is the native thread of the operating system, and the pthread in Linux is completely scheduled and executed by the operating system.
Pthread itself is not thread-safe, and users need to use locks to achieve the safe operation of multi-threads, so the Python implementation of multi-threads under the CPython interpreter must have the problem of thread unsafety.
This lays a hidden danger for the use of GIL in the multi-core era.
Background and challenges of 4.GIL
Python was released by Guido van Rossum in 1989, when the main frequency of computers had not yet reached 1G, and all programs were run on single-core computers. Multi-core processors were not developed by Intel until 2005.
Release timeline of each version of Python:
4.1 impact of multicore on software system
Gordon Moore predicted in 1965 that the number of components per integrated circuit would double every 18 to 24 months, and its applicability is expected to last until 2015-20.
Before the failure of Moore's Law, the software system can improve the performance simply with the help of the progress of hardware or only need a small amount of improvement to enjoy the performance leap.
However, since 2005, the increase in clock rate and the growth in the number of transistors are no longer in sync.
Due to the limitations of the physical properties of the processor material, the clock rate has stopped growing or even decreasing, and processor manufacturers have begun to package more execution unit cores into a single chip.
This trend puts more and more pressure on application development and programming language design.
Programmers and programming language decision makers have to consider how to quickly adapt to multi-core hardware to improve software performance and market share of programming languages, and Python is no exception.
4.2 impact of polynucleation on CPython
In the single-core era, advocate beautiful, clear, simple Guido. Van Rothum chose to implement a global mutex at the interpreter level to protect Python objects to achieve utilization of single-core CPU, which worked well in the single-core era.
If GIL is not selected in a single core, then developers need to manage their own tasks, which will not be able to maximize the utilization of CPU.
The picture shows Guido, the father of Python. Van Rothum:
However, with the advent of the multi-core era, the effective way to make efficient use of CPU core is to use parallelism. Multithreading is a good way to fully achieve parallelism, but the GIL of CPython hinders the use of multi-core CPU.
4. 3 painful and happy GIL
GIL of CPython brings convenience to users, and many important Package and language functions are developed on the basis of GIL.
However, the ubiquity of multi-core CPU and the impact of other languages on Python make GIL seem primitive and rough, which makes it a disadvantage that it is unable to make effective use of multi-core processors.
5. The problems exposed by GIL in the multicore era
To understand the impact of GIL on multithreaded programs, it is necessary to understand the basic principles of GIL operation.
Single-core CPU situation
The Pthread of CPython is scheduled and executed by operating system scheduling algorithm.
Every time the Python interpreter executes a certain amount of bytecode or encounters a system IO, it forcibly releases the GIL, and then triggers the thread scheduling of the operating system to make full use of the single-core CPU, and the time interval between release and re-execution on the single core is very short.
Multicore CPU situation
In the case of multi-core multi-threaded execution, one thread releases GIL after CPU-An execution, and threads on other CPU compete, but CPU-A may get the GIL immediately.
This causes other awakened threads on the CPU to watch eagerly as the thread on the CPU-An executes again, while they have to wait until they are switched to the scheduled state.
This will lead to frequent thread switching in multi-core CPU, which consumes resources, but only one thread can get GIL to actually execute Python code, which leads to the efficiency of multi-thread is not as high as that of single-thread execution in the case of multi-core CPU.
This situation is very similar to the shock phenomenon caused by multiple threads listening on the same port in network programming, but at the CPU level, resulting in more extravagant waste.
The actual impact of 6.GIL
Ipaw O-intensive
When executing multithreading on a single-core CPU, the interpreter achieves an efficient switch, which is beneficial.
The performance of multi-threaded programs under the control of GIL is not as bad as you might think, even if you use multi-threaded programs under the control of web crawlers, such as web crawlers.
CPU intensive
For the CPU-intensive computing program GIL, there is a big problem, because the CPU-intensive program itself does not have much waiting, does not need the intervention of the interpreter, and all tasks can only wait for one core, other core idle can not be used, so the use of multicore is really bad.
7. Abandon and optimize GIL
GIL has always been controversial, for which PEP has repeatedly tried to delete or optimize GIL, but the complexity of the interpreter itself and many class libraries under GIL make GIL removal a distant idea.
Remove GIL
In 1999, for Python 1.5, a free threading patch from Greg Stein tried to implement this idea.
In this patch, GIL is completely removed and replaced with fine-grained locks. However, the removal of GIL brings a certain price to the execution speed of single-threaded programs.
When executed with a single thread, the speed is reduced by about 40%. The use of two threads shows an increase in speed, but apart from this improvement, the benefit does not increase linearly with the increase in the number of cores. Due to the reduction in execution speed, this patch was rejected and almost forgotten.
Multicore was a fantasy in 1999, but it is also extremely difficult to remove GIL today, and the actual effect of removal is unknown, let's just say it's too hard to turn back.
Optimize GIL
In 2009, Antoine Pitrou implemented a new GIL in Python 3.2 with some positive results.
This is a major change in GIL, where the old GIL counted the Python instructions to determine when to discard GIL.
A single Python instruction will contain a lot of work. In the new GIL implementation, a fixed timeout is used to indicate the current thread to abandon the lock, making the switching between threads more predictable.
Solutions to 8.GIL defects
As a popular language with strong vitality, python will never sit idly by in the multi-core era. Even with the limitations of GIL, there are still many ways for programs to embrace multicore.
Multiple processes
Python2.6 introduces MultiProcess library to make up for the defects caused by GIL in Threading library. Based on this, a multi-process program is developed, each process has a separate GIL, which avoids the competition for GIL among multiple processes, thus realizing the utilization of multi-cores, but it also brings some synchronization and communication problems, which is inevitable.
Ctypes
The advantage of CPython is the combination with C module, so we can transfer the calculation by calling C dynamic library with the help of Ctypes. C dynamic library can not use multi-core without GIL.
Cooperative process
Collaborative program is also a good means, there is no support for collaborative program before Python3.4, there are some implementation of three-party libraries, such as gevent and Tornado.
After Python3.4, there is a built-in asyncio standard library that really implements the feature of collaboration.
9. Summary
GIL is still the most difficult technical challenge in the Python language. The problem with GIL is not the programming language itself. Other languages just shift the problem to the user level. On the contrary, the authors of Python try to shift the problem to the interpreter to present an elegant language to the user.
Although the advent of the multicore era has exposed the shortcomings of GIL, Python decision makers and community developers have taken many other measures to embrace multicore, and it is unwise to criticize GIL ignorantly.
Just as the relations of production should adapt to the development of productive forces, it is biased to put aside the historical background and talk about the advantages and disadvantages of the mechanism, so we should treat GIL dialectically.
Thank you for your reading, the above is the "Python multi-thread can not use multi-core reason is what" the content, after the study of this article, I believe you can not use multi-core Python multi-thread is what is the reason for this problem has a deeper understanding, the specific use of the need for you to practice verification. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.