본문 바로가기

Language/Python

Python GIL(Global Interpreter Lock)

about Global Interpreter Lock in python.
korean version

GIL

  • what is the GIL?
    • Global Interpreter Lock is a mutex that allows only one thread to hold the control of the python interpreter
  • how does python works?
    1. interpreter is loaded in memory
    2. python code is run through interpreter
  • python's memory management
    • python object's creation and destruction is managed through reference count
    • refcount is attribute that object have and keeps track of the number of refrence that point to object
    • when refcount reaches to zero, the memory occupied by the object is released.
  • why use GIL?
    • this reference count variable needed protection from race conditions where two threads increase or decrease its value simultaneously, so always lock when increase or decrease refcount.
      but python is everythng consist of object so inefficient.
    • Therefore python is using GIL.
  • advance
    • generally using multiprocessing modules about asynchronous problem.
      but there is somthing special about CPU usage.
      this part will be updated later.