Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of the principle of GIL Mechanism in Python

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "Analysis of GIL Mechanism Principle in Python". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian to study and learn "Analysis of GIL Mechanism Principle in Python" together.

abstract

What is Thread Safety? Why does Python use the GIL mechanism?

With the advent of the multi-core era, programming methods based on multithreading to make full use of hardware are also constantly developing, but once multithreading is involved, it will inevitably involve a concept, that is, thread safety. This article mainly talks about some of the author's understanding of thread safety.

Python for many people complain about the point is GIL, so why python choose to use GIL, this article also on this issue for some discussion.

introduced

Is your PC or laptop single-core? If so, you are out.

With the continuous progress of nanotechnology, the technology of computer chips is also progressing, but it is difficult to improve the technology to improve the speed of operation and meet Moore's theorem, so Intel, AMD have adopted horizontal expansion that is to increase more CPU, so that dual-core, 4-core, N-core continue to be introduced, so we entered the multi-core era.

So the question arises, what does the advent of multicore mean for us programmers, and how do we take advantage of multicore?

Before answering this question, I suggest readers unfamiliar with processes and threads fill in the blanks.

Of course, the program is, you can use multi-process, you can also use multi-thread. The biggest difference between the two is, whether shared resources, the latter is shared resources, and the former is independent. So you may also be thinking about why google chrome started using separate processes for each tab service (not sharing data, meaning better security).

One of the biggest problems in multithreaded environments relative to the lightweight nature of processes is how to guarantee resource contention, deadlocks, data modifications, etc.

Therefore, there is thread safety (thread safety) proposed.

thread-safe

Thread safety is a computer programming concept applicable in the context of multi-threaded programs.

A piece of code is thread-safe if it functions correctly during simultaneous execution by multiple threads.

In particular, it must satisfy the need for multiple threads to access the same shared data,

and the need for a shared piece of data to be accessed by only one thread at any given time.

In other words, thread safety ensures that programs run correctly when multiple threads are executing simultaneously, and that shared data can be accessed by multiple threads, but only one thread can access it at a time.

Since there must be competition for resources in a multithreaded environment, how can we ensure that only one thread accesses a shared resource at a time?

Locking ensures that access operations are unique, thus ensuring that only one thread accesses shared data at a time.

There are also two types of locks with different granularity:

fine-grained, so programmers need to add and unlock themselves to ensure thread safety.

coarse-grained, then the language level itself maintains a global locking mechanism to ensure thread safety.

The former is typically java, Jython, etc., and the latter is typically CPython (Python).

The former is not discussed in this article, specific reference java in the multithreaded programming section.

As for Python's global locking mechanism, also known as GIL (Global Interpreter Lock), the following mainly for some discussion.

GIL

What is GIL ? The answer can be found in Wikipedia, which simply states:

Each interpreter process can only be executed by one thread at a time, acquiring the associated lock and accessing the associated resource.

It is easy to see, then, that if an interpreter process can only have one thread to execute, multithreaded concurrency becomes impossible, even if there is no competition for resources between these threads.

Theoretically, we want to make our programs as parallel as possible, taking full advantage of multicore capabilities, so why would Python use global GIL to limit this parallelism?

This problem has been discussed a lot, more than ten years, and can be found in the following documents:

Voices against GIL:

An open letter to Guido van Rossum

GIL cannot be removed:

It isn't Easy to Remove the GIL

Other discussions are easily searchable from Google, such as: GIL at google.

So, a brief summary of both sides.

Think GIL should be removed:

Does not conform to the development trend of computers (multi-core era has arrived, and GIL will affect the use of multi-core)

Greatly increase the speed of multithreaded programs

Believes that GIL should not be removed (if removed, will):

Python extensions (modules) will encounter locking problems, programmers need to add cumbersome unlocking to ensure thread safety

Will significantly reduce the speed of single-threaded programs

The latter is Guido's biggest concern and the most important reason for not removing GIL. A simple attempt in 1999 (ten years ago) resulted in a nearly twofold reduction in the speed of single-threaded programs.

In the final analysis, it is actually a multi-process and multi-threaded choice problem. There is a paragraph that is more interesting. You can refer to http://www.artima.com/forums/flat.jsp?. forum=106&thread=214235.

I quote:

I actually don't think removing the GIL is a good solution.

But I don't think threads are a good solution, either.

They're too hard to get right, and I say that after spending literally years studying threading in both C++ and Java.

Brian Goetz has taken to saying that no one can get threading right.

Bruce Eckel's reply to Guido. And Bruce Eckel, if you know Java or C++, you probably know him.

personal point of view

So, from my point of view (I don't have much multithreaded programming experience), regardless of the speed advantage of multithreading, etc., I prefer multi-process:

Simple, no manual (or language level) unlocks required. Think about multithreaded programming in java, where programmers usually make mistakes (java programmers can think about it)

Security, which is one of the reasons why browsers started using multiple processes

Simplicity is an important principle in Python's philosophy, so it's easy to understand how to use GIL.

Of course you really need to take advantage of the speed advantage of multicore, Python may not be your best choice at this time, please consider other languages, such as Java, Erlang, etc.

Thank you for reading, the above is the content of "GIL mechanism principle analysis in Python". After studying this article, I believe everyone has a deeper understanding of the problem of GIL mechanism principle analysis in Python. The specific use situation still needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report