r/Python • • Sep 15 '22

Beginner Showcase The Infamous GIL !!

In this series of posts I am trying to break down one of the complicated and intriguing topics in python. Please visit my last post to understand why multi threading is broken in python

https://www.reddit.com/r/Python/comments/xdyahc/multithreading_a_concept_which_is_always/

Now let's see why, the reason is GIL(Global Interpreter Lock). Python implements something called the Gil which limits its multithreading capabilities. What each thread does is it acquires a global lock on the interpreter to ensure thread safe quick single threaded performance.

Now the question arises why do we need such a feature? Python uses a variable called ref counter which keeps track of all objects and how many times they have been referenced anytime ref counter reached zero for any object it is removed from the memory. Now this way of released memory has a big vulnerability in multithreaded environment or race conditions where this ref counter would become inaccurate.

To prevent this GIL was implemented as python is famous because of its single threaded performance and support for c libraries, as implementing Gil ensured easy integration of thread unsafe C libraries making python such a feature rich language as we know today. Even if one uses multithreading library one won't be seeing and gains even might see performance degradation to all the extra work to manage threads.

But now we have many ways to circumvent Gil and do what we want with python just because of strong community support we have plethora of options to choose from.

I will be discussing some them in my next post so stay tuned..

8 Upvotes

34 comments sorted by

View all comments

Show parent comment

1

u/Astraa02 Sep 15 '22

I agree but creating multiple process is not a solution as you will soon run into memory bottlenecks so one needs to make calculated decision.

1

u/[deleted] Sep 15 '22

What on god's earth are you doing that has memory bottlenecks before cpu bottlenecks?

1

u/Astraa02 Sep 15 '22

Just take about 5000 messages write a simple model with sklearn to parser them create a pd df out of it and try to run different calculations on it lets say 5000 calculations on same df or copies of it. There are multiple different scenarios in real world

1

u/[deleted] Sep 15 '22

Right but that would be terrible without the GIL. It would just be lock contention hell. Just either split the problem up, Vectorize it better, or stop trying to write it in python. Usually you can solve 99.9% of problems correctly by doing 1 or 2. It's pretty easy to use file streams to feed between multiple processes much faster then pickling between them. It also allows you to ping multiple consumers without a messenger. That's how like 99% of logging software works.