🕷️ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 79 (from laksa048)

2. Crawled Status Check

Query:
Response:

3. Robots.txt Check

Query:
Response:

4. Spam/Ban Check

Query:
Response:

5. Seen Status Check

ℹ️ Skipped - page is already crawled

đź“„
INDEXABLE
âś…
CRAWLED
6 days ago
🤖
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0.2 months ago
History dropPASSisNull(history_drop_reason)No drop reason
Spam/banPASSfh_dont_index != 1 AND ml_spam_score = 0ml_spam_score=0
CanonicalPASSmeta_canonical IS NULL OR = '' OR = src_unparsedNot set

Page Details

PropertyValue
URLhttps://towardsdatascience.com/python-gil-e63f18a08c65/
Last Crawled2026-04-15 18:27:47 (6 days ago)
First Indexed2025-04-07 07:06:51 (1 year ago)
HTTP Status Code200
Content
Meta TitleWhat is the Python Global Interpreter Lock (GIL)? | Towards Data Science
Meta Descriptionnull
Meta Canonicalnull
Boilerpipe Text
Introduction In one of my recent articles, I discussed about a few fundamental concepts in programming namely concurrency , parallelism , multi-threading and multi-processing and how they can be implemented in Python . One of the most controversial topics in this context, is definitely Python’s Global Interpreter Lock that essentially protects the interpreter which -in Python- is not thread-safe. In today’s article we will revisit threading and multi-processing and introduce Global Interpreter Lock. Additionally, we will discuss about the limitations imposed by GIL and how we can potentially find workarounds. We will also discuss about a few relevant concepts such as thread-safety and race conditions. The Global Interpreter Lock In CPython , the Global Interpreter Lock (GIL) is a mutex that allows only one thread at a time to have the control of the Python interpreter. In other words, the lock ensures that only one thread is running at any given time . Therefore, it is impossible to take advantage of multiple processors with threads. GIL , is a mutex that protects access to Python objects, preventing multiple threads from executing Python bytecodes at once – Python Wiki Since the CPython’s memory management is not thread-safe , the GIL prevents race conditions and ensures thread safety . Threads in Python share the same memory – this means that when multiple threads are running at the same time we don’t really know the precise order in which the threads will be accessing the shared data. Thread Safety and Race Conditions A thread-safe code manipulates shared data in a way that it does not interfere with other threads. Therefore, with only one thread running at a time, GIL ensures there will never be race conditions . In order to better understand what a race condition is, let’s consider a threading example where we have a shared variable called x : x = 10 Now let’s suppose that two threads are running, performing the operations outlined below: # Thread 1 x += 10 # Thread 2 x *= 5 Now depending on the order the threads will be accessing the shared variable x we may end up with different results. For example, if we assume that Thread 1 accesses the shared variable x first, the result would be 100 . x += 10 # Thread 1: x = 20 x *= 5 # Thread 2: x = 100 Alternatively, if Thread 2 accesses x first, the result would be different: x *= 5 # Thread 2: x = 50 x += 10 # Thread 1: x = 60 There’s even a third scenario where both Thread 1 and 2 read the shared variable at exactly the same time. In this case, they will both read in the initial value of x (which is equal to 10 ) and depending on which thread will write its result last, the resulting value of x will either be 20 (if Thread 1 writes its result last) or 50 (if the second thread writes its result last). This is an example of what we call a race condition . In other words, a race condition occurs when the behaviour of the system or code relies on the sequence of execution that is defined by uncontrollable events. And this is exactly what the CPython GIL does. It prevents race conditions by ensuring that only a single thread is running at any given time. This makes life easier for some Python programmers but at the same time it imposes a limitation since multi-core systems cannot be exploited in the context of threading. Threading vs Multi-processing in Python As mentioned already, a Python process cannot run threads in parallel but it can run them concurrently through context switching during I/O bound operations. Note that parallelism and concurrency may sound equivalent terms but in practise they are not. The diagram below illustrates how two threads are being executed and how context switching works when using the [threading](https://docs.python.org/3/library/threading.html) Python library. Threading in Python (Concurrency) – Source: Author Now if you want to take advantage of the computational power of multi-processor and multi-core systems, you may need to take a look at the [multiprocessing](https://docs.python.org/3/library/multiprocessing.html) package that allows processes to be executed in parallel . This is typically useful when it comes to executing CPU-bound tasks . Multi-processing in Python (Parallelism) – Source: Author Multi-processing side-steps the Global Interpreter Lock as it allows for every process spawned to have its own interpreter and thus its own GIL. For a more comprehensive read around multi-processing and threading in Python, make sure to read the related article shared below that is essentially a deeper dive into concurrency and parallelism with Python. Multi-threading and Multi-processing in Python Sidestepping the GIL in Python It is important to mention that some alternative Python implementations namely Jython and IronPython have no Global Interpreter Lock and thus they can take advantage of multiprocessor systems. Now going back to CPython, even though for many programmers the concept of GIL is quite convenient as it makes things much easier. Furthermore, developers don’t really have to interact (or even come across) with the Global Interpreter Lock unless they need to write in C extension. In this situation, you have to release the GIL when the extension does blocking I/O so that other threads within the process can take over and get executed. The concept of GIL in general though is definitely not ideal given that in certain scenarios modern multiprocessor systems cannot be fully exploited. At the same time though, many long-running or blocking operations are being executed outside the GIL . Such operations include I/O, image processing and NumPy number crunching. Therefore, a GIL becomes a bottleneck only in multithreaded operations that spend time inside the GIL itself . Getting rid of the Global Interpreter Lock is a common discussion within the Python community. It’s definitely not an easy task to replace GIL as these properties and requirements need to be met . However, Sam Gross has recently came up with a proof-of-concept implementation of CPython that supports multithreading without the global interpreter lock that is called [nogil](https://github.com/colesbury/nogil/) . This PoC essentially demonstrates how to remove GIL in a way that the CPyhton interpreter can scale with added CPU cores. You can find more details and answers to potential questions in this write-up from the author of nogil . Even if this could be a potential solution, don’t expect that any change will happen any time soon though. Final Thoughts In today’s article we discussed about one of the most controversial topics around Python, namely Global Interpreter Lock (aka GIL). We’ve seen what purpose it serves and why it was implemented in the first place but additionally, we discussed the limitations that are imposed as a result of its presence. Furthermore, we discussed about thread-safety and went through an example to demonstrate what a race condition is and how GIL prevents it. Finally, we discussed about potential ways in which you can eventually sidestep Python GIL and achieve true parallelism by taking advantage of multi-core systems. Become a member and read every story on Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium. Join Medium with my referral link – Giorgos Myrianthous You may also like Augmented Assignments in Python Dynamic Typing in Python
Markdown
[![Towards Data Science](https://towardsdatascience.com/wp-content/uploads/2025/02/TDS-Vector-Logo.svg)](https://towardsdatascience.com/) Publish AI, ML & data-science insights to a global community of data professionals. [Sign in]() [Submit an Article](https://contributor.insightmediagroup.io/) - [Latest](https://towardsdatascience.com/latest/) - [Editor’s Picks](https://towardsdatascience.com/tag/editors-pick/) - [Deep Dives](https://towardsdatascience.com/tag/deep-dives/) - [Newsletter](https://towardsdatascience.com/tag/the-variable/) - [Write For TDS](https://towardsdatascience.com/submissions/) Toggle Mobile Navigation - [LinkedIn](https://www.linkedin.com/company/towards-data-science/?originalSubdomain=ca) - [X](https://x.com/TDataScience) Toggle Search [Data Science](https://towardsdatascience.com/category/data-science/) # What is the Python Global Interpreter Lock (GIL)? Understanding the purpose of Global Interpreter Lock in Python and how it impacts multi-threading [Giorgos Myrianthous](https://towardsdatascience.com/author/gmyrianthous/) Feb 3, 2022 6 min read Share ![Photo by Osman Rana on Unsplash](https://towardsdatascience.com/wp-content/uploads/2022/01/1ajSwGDgJm1hjaJSQKREOVw-scaled.jpeg) Photo by [Osman Rana](https://unsplash.com/@osmanrana?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/s/photos/lines?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) ### Introduction In one of my recent articles, I discussed about a few fundamental concepts in programming namely *concurrency*, *parallelism*, *multi-threading* and *multi-processing* and [how they can be implemented in Python](https://towardsdatascience.com/multithreading-multiprocessing-python-180d0975ab29). One of the most controversial topics in this context, is definitely Python’s **Global Interpreter Lock** that essentially protects the interpreter which -in Python- is not thread-safe. In today’s article we will revisit threading and multi-processing and introduce Global Interpreter Lock. Additionally, we will discuss about the limitations imposed by GIL and how we can potentially find workarounds. We will also discuss about a few relevant concepts such as thread-safety and race conditions. *** ### The Global Interpreter Lock In **CPython**, the Global Interpreter Lock (GIL) is a mutex that allows only one thread at a time to have the control of the Python interpreter. In other words, the lock ensures that **only one thread is running at any given time**. Therefore, it is impossible to take advantage of multiple processors with threads. > **GIL**, is a mutex that protects access to Python objects, preventing multiple threads from executing Python bytecodes at once – [Python Wiki](https://wiki.python.org/moin/GlobalInterpreterLock) Since the **CPython’s memory management is not thread-safe**, the **GIL** prevents race conditions and **ensures thread safety**. Threads in Python share the same memory – this means that when multiple threads are running at the same time we don’t really know the precise order in which the threads will be accessing the shared data. *** ### Thread Safety and Race Conditions A thread-safe code manipulates shared data in a way that it does not interfere with other threads. Therefore, with only one thread running at a time, GIL ensures there will never be **race conditions**. In order to better understand what a race condition is, let’s consider a threading example where we have a shared variable called `x`: ``` x = 10 ``` Now let’s suppose that two threads are running, performing the operations outlined below: ``` # Thread 1 x += 10 ``` ``` # Thread 2 x *= 5 ``` Now depending on the order the threads will be accessing the shared variable `x` we may end up with different results. For example, if we assume that `Thread 1` accesses the shared variable `x` first, the result would be `100`. ``` x += 10 # Thread 1: x = 20 x *= 5 # Thread 2: x = 100 ``` Alternatively, if `Thread 2` accesses `x` first, the result would be different: ``` x *= 5 # Thread 2: x = 50 x += 10 # Thread 1: x = 60 ``` There’s even a third scenario where both Thread 1 and 2 read the shared variable at exactly the same time. In this case, they will both read in the initial value of `x` (which is equal to `10`) and depending on which thread will write its result last, the resulting value of `x` will either be `20` (if Thread 1 writes its result last) or `50` (if the second thread writes its result last). This is an example of what we call a **race condition**. In other words, a race condition occurs when the behaviour of the system or code relies on the sequence of execution that is defined by uncontrollable events. And this is exactly what the CPython GIL does. It prevents race conditions by ensuring that only a single thread is running at any given time. This makes life easier for some Python programmers but at the same time it imposes a limitation since multi-core systems cannot be exploited in the context of threading. *** ### Threading vs Multi-processing in Python As mentioned already, a Python process cannot run threads in parallel but it can run them concurrently through context switching during I/O bound operations. Note that parallelism and concurrency may sound equivalent terms but in practise they are not. The diagram below illustrates how two threads are being executed and how context switching works when using the `[threading](https://docs.python.org/3/library/threading.html)` Python library. ![Threading in Python (Concurrency) - Source: Author](https://towardsdatascience.com/wp-content/uploads/2022/02/0uv_48CTvJO4aasbT.png) Threading in Python (Concurrency) – Source: Author Now if you want to take advantage of the computational power of multi-processor and multi-core systems, you may need to take a look at the `[multiprocessing](https://docs.python.org/3/library/multiprocessing.html)` package that allows processes to be executed **in parallel**. This is typically useful when it comes to **executing CPU-bound tasks**. ![Multi-processing in Python (Parallelism) - Source: Author](https://towardsdatascience.com/wp-content/uploads/2022/02/0absCRPVFExZZkTVC.png) Multi-processing in Python (Parallelism) – Source: Author Multi-processing side-steps the Global Interpreter Lock as it allows for every process spawned to have its own interpreter and thus its own GIL. For a more comprehensive read around **multi-processing and threading** in Python, make sure to read the related article shared below that is essentially a deeper dive into **concurrency** and **parallelism** with Python. > [**Multi-threading and Multi-processing in Python**](https://towardsdatascience.com/multithreading-multiprocessing-python-180d0975ab29) *** ### Sidestepping the GIL in Python It is important to mention that some alternative Python implementations namely [Jython](https://wiki.python.org/moin/Jython) and [IronPython](https://wiki.python.org/moin/IronPython) have no Global Interpreter Lock and thus they can take advantage of multiprocessor systems. Now going back to CPython, even though for many programmers the concept of GIL is quite convenient as it makes things much easier. Furthermore, developers don’t really have to interact (or even come across) with the Global Interpreter Lock unless they need to write in C extension. In this situation, you have to release the GIL when the extension does blocking I/O so that other threads within the process can take over and get executed. The concept of GIL in general though is definitely not ideal given that in certain scenarios modern multiprocessor systems cannot be fully exploited. At the same time though, many long-running or blocking operations are being **executed outside the GIL**. Such operations include I/O, image processing and NumPy number crunching. Therefore, a GIL becomes a bottleneck only in multithreaded operations that **spend time inside the GIL itself**. Getting rid of the Global Interpreter Lock is a common discussion within the Python community. It’s definitely not an easy task to replace GIL as [these properties and requirements need to be met](https://wiki.python.org/moin/GlobalInterpreterLock). However, Sam Gross has recently came up with a proof-of-concept implementation of CPython that supports multithreading without the global interpreter lock that is called `[nogil](https://github.com/colesbury/nogil/)`. This PoC essentially demonstrates how to remove GIL in a way that the CPyhton interpreter can scale with added CPU cores. You can find more details and answers to potential questions in [this write-up](https://docs.google.com/document/d/18CXhDb1ygxg-YXNBJNzfzZsDFosB5e6BfnXLlejd9l0) from the author of `nogil`. Even if this could be a potential solution, don’t expect that any change will happen any time soon though. *** ### Final Thoughts In today’s article we discussed about one of the most controversial topics around Python, namely Global Interpreter Lock (aka GIL). We’ve seen what purpose it serves and why it was implemented in the first place but additionally, we discussed the limitations that are imposed as a result of its presence. Furthermore, we discussed about thread-safety and went through an example to demonstrate what a race condition is and how GIL prevents it. Finally, we discussed about potential ways in which you can eventually sidestep Python GIL and achieve true parallelism by taking advantage of multi-core systems. *** **[Become a member](https://gmyrianthous.medium.com/membership) and read every story on Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium.** > [**Join Medium with my referral link – Giorgos Myrianthous**](https://gmyrianthous.medium.com/membership) *** **You may also like** > [**Augmented Assignments in Python**](https://towardsdatascience.com/augmented-assignments-python-caa4990811a0) *** > [**Dynamic Typing in Python**](https://towardsdatascience.com/dynamic-typing-in-python-307f7c22b24e) *** Written By Giorgos Myrianthous [See all from Giorgos Myrianthous](https://towardsdatascience.com/author/gmyrianthous/) [Data Science](https://towardsdatascience.com/tag/data-science/), [Machine Learning](https://towardsdatascience.com/tag/machine-learning/), [Programming](https://towardsdatascience.com/tag/programming/), [Python](https://towardsdatascience.com/tag/python/), [Software Development](https://towardsdatascience.com/tag/software-development/) Share This Article - [Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Ftowardsdatascience.com%2Fpython-gil-e63f18a08c65%2F&title=What%20is%20the%20Python%20Global%20Interpreter%20Lock%20%28GIL%29%3F) - [Share on LinkedIn](https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Ftowardsdatascience.com%2Fpython-gil-e63f18a08c65%2F&title=What%20is%20the%20Python%20Global%20Interpreter%20Lock%20%28GIL%29%3F) - [Share on X](https://x.com/share?url=https%3A%2F%2Ftowardsdatascience.com%2Fpython-gil-e63f18a08c65%2F&text=What%20is%20the%20Python%20Global%20Interpreter%20Lock%20%28GIL%29%3F) Towards Data Science is a community publication. Submit your insights to reach our global audience and earn through the TDS Author Payment Program. [Write for TDS](https://towardsdatascience.com/questions-96667b06af5/) ## Related Articles - ![](https://towardsdatascience.com/wp-content/uploads/2024/08/0c09RmbCCpfjAbSMq.png) ## [Implementing Convolutional Neural Networks in TensorFlow](https://towardsdatascience.com/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34/) [Artificial Intelligence](https://towardsdatascience.com/category/artificial-intelligence/) Step-by-step code guide to building a Convolutional Neural Network [Shreya Rao](https://towardsdatascience.com/author/shreya-rao/) August 20, 2024 6 min read - ![Photo by Krista Mangulsone on Unsplash](https://towardsdatascience.com/wp-content/uploads/2024/08/0GyVVTbgotH-DhGPH-scaled.jpg) ## [How to Forecast Hierarchical Time Series](https://towardsdatascience.com/how-to-forecast-hierarchical-time-series-75f223f79793/) [Artificial Intelligence](https://towardsdatascience.com/category/artificial-intelligence/) A beginner’s guide to forecast reconciliation [Dr. Robert Kübler](https://towardsdatascience.com/author/dr-robert-kuebler/) August 20, 2024 13 min read - ![Photo by davisuko on Unsplash](https://towardsdatascience.com/wp-content/uploads/2024/08/1bAABgtZtAIG5YW1oEjW3pA-scaled.jpeg) ## [Hands-on Time Series Anomaly Detection using Autoencoders, with Python](https://towardsdatascience.com/hands-on-time-series-anomaly-detection-using-autoencoders-with-python-7cd893bbc122/) [Data Science](https://towardsdatascience.com/category/data-science/) Here’s how to use Autoencoders to detect signals with anomalies in a few lines of… [Piero Paialunga](https://towardsdatascience.com/author/piero-paialunga/) August 21, 2024 12 min read - ![Image from Canva.](https://towardsdatascience.com/wp-content/uploads/2024/08/1UAA9jQVdqMXnwzYiz8Q53Q.png) ## [3 AI Use Cases (That Are Not a Chatbot)](https://towardsdatascience.com/3-ai-use-cases-that-are-not-a-chatbot-f4f328a2707a/) [Machine Learning](https://towardsdatascience.com/category/artificial-intelligence/machine-learning/) Feature engineering, structuring unstructured data, and lead scoring [Shaw Talebi](https://towardsdatascience.com/author/shawhin/) August 21, 2024 7 min read - ## [Solving a Constrained Project Scheduling Problem with Quantum Annealing](https://towardsdatascience.com/solving-a-constrained-project-scheduling-problem-with-quantum-annealing-d0640e657a3b/) [Data Science](https://towardsdatascience.com/category/data-science/) Solving the resource constrained project scheduling problem (RCPSP) with D-Wave’s hybrid constrained quadratic model (CQM) [Luis Fernando PÉREZ ARMAS, Ph.D.](https://towardsdatascience.com/author/luisfernandopa1212/) August 20, 2024 29 min read - ![](https://towardsdatascience.com/wp-content/uploads/2023/02/1VEUgT5T4absnTqBMOEuNig.png) ## [Back To Basics, Part Uno: Linear Regression and Cost Function](https://towardsdatascience.com/back-to-basics-part-uno-linear-regression-cost-function-and-gradient-descent-590dcb3eee46/) [Data Science](https://towardsdatascience.com/category/data-science/) An illustrated guide on essential machine learning concepts [Shreya Rao](https://towardsdatascience.com/author/shreya-rao/) February 3, 2023 6 min read - ![](https://towardsdatascience.com/wp-content/uploads/2024/08/1kM8tfYcdaoccB1HX71YDig.png) ## [Must-Know in Statistics: The Bivariate Normal Projection Explained](https://towardsdatascience.com/must-know-in-statistics-the-bivariate-normal-projection-explained-ace7b2f70b5b/) [Data Science](https://towardsdatascience.com/category/data-science/) Derivation and practical examples of this powerful concept [Luigi Battistoni](https://towardsdatascience.com/author/lu-battistoni/) August 14, 2024 7 min read - [YouTube](https://www.youtube.com/c/TowardsDataScience) - [X](https://x.com/TDataScience) - [LinkedIn](https://www.linkedin.com/company/towards-data-science/?originalSubdomain=ca) - [Threads](https://www.threads.net/@towardsdatascience) - [Bluesky](https://bsky.app/profile/towardsdatascience.com) [![Towards Data Science](https://towardsdatascience.com/wp-content/uploads/2025/02/TDS-Vector-Logo.svg)](https://towardsdatascience.com/) Your home for data science and Al. The world’s leading publication for data science, data analytics, data engineering, machine learning, and artificial intelligence professionals. © Insight Media Group, LLC 2026 Subscribe to Our Newsletter - [Write For TDS](https://towardsdatascience.com/questions-96667b06af5/) - [About](https://towardsdatascience.com/about-towards-data-science-d691af11cc2f/) - [Advertise](https://contact.towardsdatascience.com/advertise-with-towards-data-science) - [Privacy Policy](https://towardsdatascience.com/privacy-policy/) - [Terms of Use](https://towardsdatascience.com/website-terms-of-use/) ![](https://px.ads.linkedin.com/collect/?pid=7404572&fmt=gif)
Readable Markdown
### Introduction In one of my recent articles, I discussed about a few fundamental concepts in programming namely *concurrency*, *parallelism*, *multi-threading* and *multi-processing* and [how they can be implemented in Python](https://towardsdatascience.com/multithreading-multiprocessing-python-180d0975ab29). One of the most controversial topics in this context, is definitely Python’s **Global Interpreter Lock** that essentially protects the interpreter which -in Python- is not thread-safe. In today’s article we will revisit threading and multi-processing and introduce Global Interpreter Lock. Additionally, we will discuss about the limitations imposed by GIL and how we can potentially find workarounds. We will also discuss about a few relevant concepts such as thread-safety and race conditions. *** ### The Global Interpreter Lock In **CPython**, the Global Interpreter Lock (GIL) is a mutex that allows only one thread at a time to have the control of the Python interpreter. In other words, the lock ensures that **only one thread is running at any given time**. Therefore, it is impossible to take advantage of multiple processors with threads. > **GIL**, is a mutex that protects access to Python objects, preventing multiple threads from executing Python bytecodes at once – [Python Wiki](https://wiki.python.org/moin/GlobalInterpreterLock) Since the **CPython’s memory management is not thread-safe**, the **GIL** prevents race conditions and **ensures thread safety**. Threads in Python share the same memory – this means that when multiple threads are running at the same time we don’t really know the precise order in which the threads will be accessing the shared data. *** ### Thread Safety and Race Conditions A thread-safe code manipulates shared data in a way that it does not interfere with other threads. Therefore, with only one thread running at a time, GIL ensures there will never be **race conditions**. In order to better understand what a race condition is, let’s consider a threading example where we have a shared variable called `x`: ``` x = 10 ``` Now let’s suppose that two threads are running, performing the operations outlined below: ``` # Thread 1 x += 10 ``` ``` # Thread 2 x *= 5 ``` Now depending on the order the threads will be accessing the shared variable `x` we may end up with different results. For example, if we assume that `Thread 1` accesses the shared variable `x` first, the result would be `100`. ``` x += 10 # Thread 1: x = 20 x *= 5 # Thread 2: x = 100 ``` Alternatively, if `Thread 2` accesses `x` first, the result would be different: ``` x *= 5 # Thread 2: x = 50 x += 10 # Thread 1: x = 60 ``` There’s even a third scenario where both Thread 1 and 2 read the shared variable at exactly the same time. In this case, they will both read in the initial value of `x` (which is equal to `10`) and depending on which thread will write its result last, the resulting value of `x` will either be `20` (if Thread 1 writes its result last) or `50` (if the second thread writes its result last). This is an example of what we call a **race condition**. In other words, a race condition occurs when the behaviour of the system or code relies on the sequence of execution that is defined by uncontrollable events. And this is exactly what the CPython GIL does. It prevents race conditions by ensuring that only a single thread is running at any given time. This makes life easier for some Python programmers but at the same time it imposes a limitation since multi-core systems cannot be exploited in the context of threading. *** ### Threading vs Multi-processing in Python As mentioned already, a Python process cannot run threads in parallel but it can run them concurrently through context switching during I/O bound operations. Note that parallelism and concurrency may sound equivalent terms but in practise they are not. The diagram below illustrates how two threads are being executed and how context switching works when using the `[threading](https://docs.python.org/3/library/threading.html)` Python library. ![Threading in Python (Concurrency) - Source: Author](https://towardsdatascience.com/wp-content/uploads/2022/02/0uv_48CTvJO4aasbT.png) Threading in Python (Concurrency) – Source: Author Now if you want to take advantage of the computational power of multi-processor and multi-core systems, you may need to take a look at the `[multiprocessing](https://docs.python.org/3/library/multiprocessing.html)` package that allows processes to be executed **in parallel**. This is typically useful when it comes to **executing CPU-bound tasks**. ![Multi-processing in Python (Parallelism) - Source: Author](https://towardsdatascience.com/wp-content/uploads/2022/02/0absCRPVFExZZkTVC.png) Multi-processing in Python (Parallelism) – Source: Author Multi-processing side-steps the Global Interpreter Lock as it allows for every process spawned to have its own interpreter and thus its own GIL. For a more comprehensive read around **multi-processing and threading** in Python, make sure to read the related article shared below that is essentially a deeper dive into **concurrency** and **parallelism** with Python. > [**Multi-threading and Multi-processing in Python**](https://towardsdatascience.com/multithreading-multiprocessing-python-180d0975ab29) *** ### Sidestepping the GIL in Python It is important to mention that some alternative Python implementations namely [Jython](https://wiki.python.org/moin/Jython) and [IronPython](https://wiki.python.org/moin/IronPython) have no Global Interpreter Lock and thus they can take advantage of multiprocessor systems. Now going back to CPython, even though for many programmers the concept of GIL is quite convenient as it makes things much easier. Furthermore, developers don’t really have to interact (or even come across) with the Global Interpreter Lock unless they need to write in C extension. In this situation, you have to release the GIL when the extension does blocking I/O so that other threads within the process can take over and get executed. The concept of GIL in general though is definitely not ideal given that in certain scenarios modern multiprocessor systems cannot be fully exploited. At the same time though, many long-running or blocking operations are being **executed outside the GIL**. Such operations include I/O, image processing and NumPy number crunching. Therefore, a GIL becomes a bottleneck only in multithreaded operations that **spend time inside the GIL itself**. Getting rid of the Global Interpreter Lock is a common discussion within the Python community. It’s definitely not an easy task to replace GIL as [these properties and requirements need to be met](https://wiki.python.org/moin/GlobalInterpreterLock). However, Sam Gross has recently came up with a proof-of-concept implementation of CPython that supports multithreading without the global interpreter lock that is called `[nogil](https://github.com/colesbury/nogil/)`. This PoC essentially demonstrates how to remove GIL in a way that the CPyhton interpreter can scale with added CPU cores. You can find more details and answers to potential questions in [this write-up](https://docs.google.com/document/d/18CXhDb1ygxg-YXNBJNzfzZsDFosB5e6BfnXLlejd9l0) from the author of `nogil`. Even if this could be a potential solution, don’t expect that any change will happen any time soon though. *** ### Final Thoughts In today’s article we discussed about one of the most controversial topics around Python, namely Global Interpreter Lock (aka GIL). We’ve seen what purpose it serves and why it was implemented in the first place but additionally, we discussed the limitations that are imposed as a result of its presence. Furthermore, we discussed about thread-safety and went through an example to demonstrate what a race condition is and how GIL prevents it. Finally, we discussed about potential ways in which you can eventually sidestep Python GIL and achieve true parallelism by taking advantage of multi-core systems. *** **[Become a member](https://gmyrianthous.medium.com/membership) and read every story on Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium.** > [**Join Medium with my referral link – Giorgos Myrianthous**](https://gmyrianthous.medium.com/membership) *** **You may also like** > [**Augmented Assignments in Python**](https://towardsdatascience.com/augmented-assignments-python-caa4990811a0) *** > [**Dynamic Typing in Python**](https://towardsdatascience.com/dynamic-typing-in-python-307f7c22b24e)
ML Classification
ML Categories
/Computers_and_Electronics
98.5%
/Computers_and_Electronics/Programming
94.0%
/Computers_and_Electronics/Programming/Scripting_Languages
90.4%
Raw JSON
{
    "/Computers_and_Electronics": 985,
    "/Computers_and_Electronics/Programming": 940,
    "/Computers_and_Electronics/Programming/Scripting_Languages": 904
}
ML Page Types
/Article
99.9%
/Article/Tutorial_or_Guide
80.8%
Raw JSON
{
    "/Article": 999,
    "/Article/Tutorial_or_Guide": 808
}
ML Intent Types
Informational
99.9%
Raw JSON
{
    "Informational": 999
}
Content Metadata
Languageen-us
AuthorGiorgos Myrianthous
Publish Time2022-02-03 17:44:45 (4 years ago)
Original Publish Time2022-02-03 17:44:45 (4 years ago)
RepublishedNo
Word Count (Total)1,620
Word Count (Content)1,224
Links
External Links18
Internal Links40
Technical SEO
Meta NofollowNo
Meta NoarchiveNo
JS RenderedNo
Redirect Targetnull
Performance
Download Time (ms)720
TTFB (ms)702
Download Size (bytes)35,667
Shard79 (laksa)
Root Hash12035788063718406279
Unparsed URLcom,towardsdatascience!/python-gil-e63f18a08c65/ s443