views:

294

answers:

3

I often see people talking that the GIL is per Python Interpreter (even here on stackoverflow).

But what I see in the source code it seems to be that the GIL is a global variable and therefore there is one GIL for all Interpreters in each python process. I know they did this because there is no interpreter object passed around like lua or TCL does, it was just not designed well in the beginning. And thread local storage seems to be not portable for the python guys to use.

Is this correct? I had a short look at the 2.4 version I'm using in a project here.

Had this changed in later versions, especially in 3.0?

A: 

I believe it is true (at least as of Python 2.6) that each process may have at most one CPython interpreter embedded (other runtimes may have different constraints). I'm not sure if this is an issue with the GIL per se, but it is likely due to global state, or to protect from conflicting global state in third-party C modules. From the CPython API Docs:

[Py___Initialize()] is a no-op when called for a second time (without calling Py_Finalize() first). There is no return value; it is a fatal error if the initialization fails.

You might be interested in the Unladen Swallow project, which aims eventually to remove the GIL entirely from CPython. Other Python runtimes don't have the GIL at all, like (I believe) Stackless Python, and certainly Jython.

Also note that the GIL is still present in CPython 3.x.

dcrosta
A lot of projects have removed the GIL from CPython before. Unladen Swallow isn't the first. However they didn't perform as well as the GIL version so nobody uses them.
nosklo
Also, stackless doesn't remove the GIL. In fact, any blocking operation in any stackless microthread will block the entire application.
nosklo
+2  A: 

Perhaps the confusion comes about because most people assume Python has one interpreter per process. I recall reading that the support for multiple interpreters via the C API was largely untested and hardly ever used. (And when I gave it a go, didn't work properly.)

Kylotan
+3  A: 

The GIL is indeed per-process, not per-interpreter. This is unchanged in 3.x.

Martin v. Löwis