DEV Community

Discussion on: Explain python global interpreter lock (GIL) Like I'm Five

Collapse
 
tsundara profile image
Thyag Sundararmoorthy • Edited

Imagine lunch time in your office pantry. People have their lunch boxes. They are waiting to warm their food in the microwave.

• The pantry room = process
• Lunch boxes = threads
• The microwave oven = CPU
• The food = instructions
• Warming the food = execution
• The microwave lock = The GIL

At a given time, the food in a single lunch box can be warmed. Other lunch boxes have to wait. The microwave lock prevents other lunch boxes from entering the microwave oven.

Similarly, the instructions in a single thread can be executed. Other threads have to wait. The GIL prevents other threads from entering the CPU.

This whole act of waiting is the reason why the GIL is frowned upon. The CPU itself is doing work, but the threads are idle. Hence "overall performance" takes a hit.

Hence, to improve performance, the management creates a new pantry room with its own mincrowave oven. In Python, the equivalent is creating a new process via the multiprocessing module. The flip side is that this option costs more (more resources needed )