Here we will introduce the Gil and thread safety of Python, in the hope that we can understand the Gil in Python, and the Gil's past life.
For Python Gil and thread safety Many people do not know very well, through this article, I hope to let you to Python's Gil and other content to help. This article will also focus on the author's understanding of Thread safety.
Summary
What is thread safety? Why does Python use the Gil mechanism?
In the background of the multi-core era, based on multi-thread to fully utilize the hardware programming method has been developed, but once involved in multi-threading, it will inevitably involve a concept, that is, thread safety, this article mainly on the author's understanding of Thread safety.
The point that Python complains to a lot of people is Gil, so why Python chooses to use Gil, this article also discusses this issue.
Contents
Abstract Introduction Thread Safety GIL personal Point of view reference to this article's RST source code. introduced
Is your PC or laptop still a single-core? If it is, then you are out.
With the progress of nanotechnology, the process of computer chips is also progressing, but it is difficult to improve the process in order to increase the speed of operation and to meet the Moore theorem, so Intel, AMD successively in the adoption of horizontal expansion is to add more CPUs, thus dual-core, 4-core, N-core continuously launched, so we entered the multicore era .
So a question arises, what does the advent of the multicore era mean for US programmers, and how do we take advantage of multicore?
Before answering this question, it is recommended that readers who are unfamiliar with the process and thread should be able to fill in the relevant knowledge first.
Of course, it is possible to use multiple processes or multithreading. The biggest difference between the two is whether the resource is shared , which is the shared resource, and the former is independent. So you may also be thinking about why Google Chrome is starting to use a separate process for each tab service ( No data sharing means better security ).
Compared with the lightweight characteristics of the process, there is one of the biggest problems in multithreaded environment is how to ensure the resource competition, deadlock, data modification and so on.
As a result, there is thread-safe (threading safety) proposed.
Thread Safety
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 GKFX Data,and the need for a shared Piec E of data to is accessed by only one thread at any given time.
The above is explained in Wikipedia, in other words, thread safety is in the multi-threaded environment, thread security can ensure that multiple threads concurrently execute the program is still running correctly, and to ensure that the shared data can be accessed by multiple threads, but only one thread at a time to access.
Since there must be competition for resources in a multithreaded environment, how can we ensure that only one thread accesses shared resources at the same time?
Lock, yes, lock to ensure the uniqueness of access operations, so that only one thread at a time to share data access.
There are also 2 different granularity locks that are usually added to the lock:
Fine-grained (so-called fine granularity ), then programmers need to add and unlock themselves to ensure thread safety
coarse-grained (so-called coarse-grained ), the language level itself maintains a global lock mechanism to ensure thread safety
The previous way is typically Java, Jython, etc., the latter is typically CPython (python).
The first one is not discussed in this article, but you can refer to the Multithreaded Programming section in Java.
As for the global locking mechanism in Python, also known as the GIL (global Interpreter lock), here are some main discussions.
Python's Gil
What is Gil? The answer can be referred to in Wikipedia, simply to say:
Each interpreter process can only be executed with only one thread at a time, acquiring the associated lock and accessing the associated resource.
It is easy to see that if a interpreter process can have only one thread to execute, multithreading concurrency becomes impossible, even if there is no competition for resources between the several threads.
In theory, we want to make the program as parallel as possible, to take full advantage of the multi-core function, then why should Python use the global Gil to limit this parallelism?
This problem, in fact, has been a lot of discussion, more than ten years, you can refer to the following documents:
Against Gil's voice:
An open letter to Guido van Rossum (this article is worth a look, there are a lot of comments below are also worth a look)
That the Gil cannot be removed:
It isn ' t easy to Remove the Gil (this article from Python author Guido, he explains what to use GIL)
Some of the other discussions are easy to find from Google, such as GIL at Google.
So, simply summarize the views of both sides.
Think the Gil should be removed:
Do not conform to the development trend of the computer (the multicore era has arrived, and GIL will affect the use of multicore)
Dramatically increase the speed of multi-threaded threads
That the Gil should not be removed (if removed, would be):
There is a problem with locks when writing Python extensions (module), and programmers need to be cumbersome to unlock to ensure thread safety
Will reduce the speed of single-threaded threads more drastically
The latter is Guido's most important reason for not removing Gil, a simple attempt was made in 1999 (ten years ago), and the end result is that the single-threaded program slows down almost twice times.
In the final analysis, in fact, multi-process and multi-threaded choice problem, there is a paragraph more interesting, can refer to http://www.artima.com/forums/flat.jsp?forum=106&thread=214235.
I quote as follows:
I actually don ' t think removing the GIL is a good solution.
But I don ' t think threads is a good solution, either.
They ' re too hard-get right, and I say the after spending literally years studying threading in both C + + and Java.
Brian Goetz has taken to saying, so no one can get threading right.
From Bruce Eckel's reply to Guido. And Bruce Eckel is a person, if you know Java or C + +, then should not know him.
Personal point of view
Well, from my own point of view (I don't have much multithreaded programming experience), regardless of the speed advantage of multi-threading, I prefer multi-process is:
Simple, no need for artificial (or language level) to unlock. Think of multithreaded programming in Java where programmers often make mistakes (Java programmers can think about it)
Security, which is one reason why browsers are starting to use multiple processes
According to Python's own philosophy, simplicity is a very important principle, so the use of GIL is also very well understood.
Of course you really need to take advantage of the multi-core speed advantage, when Python may not be your best choice, consider a different language, such as Java, Erlang and so on.
Title: thread Safety and Gil in Python
Links:http://www.cnblogs.com/mindsbook/archive/2009/10/15/thread-safety-and-GIL.html
Analysis of the Gil and thread safety of Python