The Python Global Interpreter Lock - Explained

Tech With Tim · Beginner ·📄 Research Papers Explained ·3y ago

Key Takeaways

The video explains the concept of the Global Interpreter Lock (GIL) in Python, its impact on parallel processing, and how it can be sidestepped using multi-processing.

Full Transcript

let's talk about one of the most controversial features in Python the G or Global interpreter lock in essence this feature disallows parallel processing and restricts your program such that it can only run one thread at a time to better understand this let's have a look at how traditional programs are executed by your CPU in 2023 a typical CPU has four cores and between four to eight threads to keep our example simple we'll imagine that each core only has one Hardware thread each CPU core is a physical processing unit that is capable of handling one task at a time the more cores we have the more tasks we can process at the same exact time the types of tasks that CPU cores handle are called software threads it can get confusing but software threads are created by applications while Hardware threads exist on the physical CPU from now on we'll only be referring to software threads now software threads are portions of applications or programs that can be run independently by a CPU core the more cores we have the more threads we can execute at the same time this means that we can speed up our programs by splitting them into multiple threads so let's take an example where we want to sum the numbers from 1 to 20 well if we do this with a single thread we're only utilizing one of our four CPU cores to speed up this operation we can split our program into four threads where each thread is responsible for summing a portion of the numbers thread 1 can sum the numbers 1 to 5 thread 2 sums 6 to 10 thread 3 three sums 11 to 15 and thread four sums 16 to 20 now that we're using four threads we can use four CPU cores and we sped up our program by four times now this adds a bit of complexity but overall it's worth the trade-off now here comes the bad news in Python it doesn't quite work this way whenever we execute code we use something called an interpreter to keep it simple this is what's capable of understanding the code and processing the instructions that we wrote that means that in the previous example each one of our threads will need to access a code interpreter whenever it's executing instructions in most languages this is fine multiple threads can use the same interpreter at the exact same time but in Python we have something called a lock now a lock is essentially a gate that only lets one thread through at a time since we have a lock on our python interpreter that means that even though we have four threads only one can use The Interpreter at a time if a thread is using The Interpreter it can release the lock allowing another thread to access it but only one thread can be using The Interpreter at the same time that in essence is the python Global interpreter lock now at this point you might be asking yourself why would I ever use multiple threads in Python well the answer is when you're working with network or input output type operations now imagine a simple example where a user visits your app the app may need to load the user's friends and chat history from a file or from some API now while this loading is happening you still want your user to be able to interact with the app if you were to play place all of this logic in one single thread the app would freeze waiting on the data to be loaded before the user can do anything else on the app or before you can load additional data now if you split this operation into three threads even though only one thread can execute at a time you can execute a different thread while one thread is waiting on a result from the file system or the server or network so what we'll do here is we'll make a thread to load the chat history a thread to load the friends and a thread that runs the user interface we start by running the chat history thread which quickly sends a request to a server for some results This Thread then hangs waiting on a result now as soon as it starts waiting we can switch to execute a different thread in this case the friend's thread same here as soon as it starts waiting we execute the user interface thread allowing the app to run while the data is loading now once we receive the data we can quickly switch back to the other threads process that data and move on this allows us to execute data concurrently so the CPU can always be working and never waiting on a result that we don't have control over creating multiple threads allows us to switch between those different threads so we're always utilizing the CPU so now that we have a good understanding of threading let's briefly discuss multi-processing python has a multiprocessing module which allows us to create separate processes now as a simplification you can imagine that each process spawns its own instance of the Python interpreter this means that we can sidestep the global interpreter lock and run a truly parallel application now multi-processing is not as easy to handle as multi-threading but this is a solid solution to avoid the global interpreter lock so now at this point you know about one of the most controversial features in Python the global interpreter lock if you want to learn more about python make sure to subscribe to this channel I look forward to seeing you in another one [Music]

Original Description

To learn programming and Python - check out Datacamp! 💻 Learn Python - https://datacamp.pxf.io/4PODjr 💻 Learn Programming - https://datacamp.pxf.io/3e6xrk Today, I'm revealing the worst feature Python has... The GIL (Global Interpreter Lock)! We'll be going over what the GIL is, how it compares to traditional programs, and why it's one of the worst features. 🎬 Timestamps⏱️ 00:00 | What is The GIL 00:18 | How Traditional Programs Work 01:44 | The Problem With Python 02:37 | Why Use Multiple Threads in Python 04:10 | Multi-Processing ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️ 👕 Merchandise: 🔗 https://teespring.com/stores/tech-with-tim-merch-shop 📸 Instagram: 🔗 https://www.instagram.com/tech_with_tim 📱 Twitter: 🔗 https://twitter.com/TechWithTimm 🔊 Discord: 🔗 https://discord.gg/twt 📝 LinkedIn: 🔗 https://www.linkedin.com/in/tim-ruscica-82631b179/ 🌎 Website: 🔗 https://techwithtim.net 📂 GitHub: 🔗 https://github.com/techwithtim One-Time Donations: 💲 https://www.paypal.com/donate?hosted_button_id=CU9FV329ADNT8 Patreon: 💲 https://www.patreon.com/techwithtim ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️ ⭐️ Tags ⭐️ - Tech With Tim - The GIL - Python ⭐️ Hashtags ⭐️ #programming #python #coding
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tech With Tim · Tech With Tim · 0 of 60

← Previous Next →
1 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

The Global Interpreter Lock (GIL) in Python prevents true parallel execution of threads, but can be sidestepped using multi-processing. This lesson explains the GIL, its impact on performance, and how to use multi-processing to achieve parallelism.

Key Takeaways
  1. Understand the concept of the GIL and its impact on threading
  2. Learn how to use multi-threading in Python
  3. Understand the limitations of multi-threading due to the GIL
  4. Learn how to use multi-processing in Python to sidestep the GIL
  5. Apply multi-processing to achieve parallelism in Python applications
💡 The GIL prevents true parallel execution of threads in Python, but multi-processing can be used to achieve parallelism by spawning separate instances of the Python interpreter.

Related Reads

📰
On July 1, 2026, arXiv will spin out from Cornell University, its home for the past 25 years, to become an independent nonprofit organization. Major funding support from Simons Foundation and Schmidt Sciences. Ditching the red for their website. [N]
arXiv is becoming an independent nonprofit organization after 25 years at Cornell University, backed by major funding, which will impact the future of research and academia
Reddit r/MachineLearning
📰
CS-NRRM™ Official Publications: Paper 1 and Paper 2 Are Now Available
Learn about the CS-NRRM's official publications on a 12-year longitudinal human observation archive and its significance in research and development
Medium · Data Science
📰
Found a potential mistake in an ICLR 2026 blogpost [D]
Verify a potential mistake in an ICLR 2026 blog post and learn how to effectively report errors in academic publications
Reddit r/MachineLearning
📰
Rebuttals Move Peer-Review Scores, but Initial-Review Structure Bounds the Movement
Learn how author rebuttals impact peer-review scores and the factors that influence their effectiveness in ICLR 2024-2025, using LLMs for measurement
ArXiv cs.AI

Chapters (5)

| What is The GIL
0:18 | How Traditional Programs Work
1:44 | The Problem With Python
2:37 | Why Use Multiple Threads in Python
4:10 | Multi-Processing
Up next
How to get started With Drug Discovery using BioAI: Computational Biology ( 4K UHD Med Masterclass )
Sudarshan's Multiverse
Watch →