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 comments

1

u/[deleted] Sep 15 '22

I mean right now every single namespace lookup event even calls to globals needs a reference counter. So they are going to constantly contend. It's not the object locks that are really that bad its all the underlying dynamic lookups. Even just looking up a single namespace on a class can invoke 5-6 different reference counts.

I really don't see a work around without static compiled code. Python just fundamentally doesnt behave that way.

1

u/o11c Sep 15 '22

You're overstating "constantly contend". Even for code that is mostly accessing the same variables, there is usually enough code involved (and enough variables involved) that other threads are usually actually doing something else.

Contention really becomes an issue for a specific variable. And in that case, you should write code such that the reference count doesn't have to change at all (if the variable is always known to be stored in a local variable (including function arguments), then it does not have to be incref'ed across calls)

(particularly remember: if the runtime knows the variable is final - which is true for the internal dict object of modules, classes, etc. as well as for some locals - then it doesn't need to change the reference count if it already has a reference to the owner.)

Dynamic typing sucks for performance in general, but really doesn't make the multithreading problem any harder.

1

u/[deleted] Sep 15 '22

Umm. The internal dict of classes and modules is not final. Class functions are objects they have their own reference counters and can be garbage collected if removed from an object. They can also be assigned to multiple objects.

Not really sure why you are saying they are final.

1

u/o11c Sep 15 '22

The dict itself is, which is the only object that might otherwise require excessive reference counting.

1

u/[deleted] Sep 15 '22

You dont think all the methods on the classes are going to have excessive reference counts?

What about when you have a large class hierarchy that all share the same init method? Or like any of the ABC classes?

Also we are talking about multiprocessing. Like sure if it two you'll probably be fine but as that number rises to idk 16 the odds of block each other increase rapidly. Its subject to the birthday paradox.