Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Understanding Python's Global Interpreter Lock (Summary)

The Python GIL is often regarded as a mysterious and difficult topic. But keep in mind that as a Pythonista you’re usually only affected by it if you are writing C extensions or if you’re using CPU-bound multi-threading in your programs.

In that case, this video course should give you everything you need to understand what the GIL is and how to deal with it in your own projects. And if you want to understand the low-level inner workings of GIL, I’d recommend you watch the Understanding the Python GIL talk by David Beazley.

Download

Sample Code (.zip)

3.3 KB
Download

Course Slides (.pdf)

2.4 MB

00:00 In the previous lesson, I covered what is being done about the GIL. This is the course summary where I’ll also point you at sources of further information.

00:08 The Global Interpreter Lock prevents race conditions in the reference counter used by CPython to manage memory. Although there would be problems without it or a similar mechanism, it does result in limiting the types of concurrency that are useful in Python.

00:23 Since the GIL is an implementation detail of the CPython interpreter, how it impacts your code can change as both it and the bytecode generated by the compiler aren’t guaranteed to be the same between versions.

00:34 There has been one failed attempt to remove the GIL known as the Gilectomy, and further work to reduce and maybe remove it is ongoing.

00:44 The PEP that talks about changing the subinterpreters is PEP 554, and the one that outlines moving the GIL to be per-subinterpreter is 684. PEP 703 is the possible beginning of the GIL finally getting removed.

01:00 This second link is a long discussion thread on the roadmap for the removal of the GIL, if you wish to learn the details.

01:08 As I mentioned, a bunch of work has been done on the sub interpreters in Python 3.12. This tutorial, originally released while 3.12 was still in beta, discusses the changes in detail.

01:19 And finally, if you want to learn more about the ins and outs of concurrency in Python, Real Python has an entire learning path devoted to the subject. It contains multiple courses and tutorials, covering everything from processes to threads to asyncio and everything in between.

01:36 The GIL often gets spoken of as a core problem in Python, even though it’s actually a solution. I hope this course taught you more, and you now better understand why it’s there and why it’s hard to remove.

01:47 Thanks for your attention.

Become a Member to join the conversation.