Python Global Interpreter Lock
GIL이 있다고 Thread Safe 하지 않음
GIL 은 스레드에서 사용되는 Lock 을 인터프리터 레벨로 확장한 개념인데 여러 스레드가 동시에 실행되는걸 방지
멀티 스레드로 만들었어도 본질적으로 싱글 스레드로 동작
더 정확히 말하자면 어느 시점이든 하나의 Bytecode 만이 실행되도록 강제
각 스레드는 다른 스레드에 의해 GIL 이 해제되길 기다린 후에야 실행
- C에서 thread를 사용할 때에 race condition이 일어나지 않도록 하는 것은 순전히 사용자의 몫이다.
- CPython은 생성되는 개체의 reference를 세어가면서 메모리 관리를 한다.
느리다
- 일단 단일 thread일 때는 아무런 문제가 없다.
- CPU가 바쁘게 계산하는 일들은 numpy/scipy에서 GIL 바깥에서 굉장히 효율적인 C 코드로 연산할 수 있다.
- 병렬 처리에 관해서는 굳이 thread가 아니더라도 multiprocessing이나 asyncio 등의 많은 선택지가 있다.
- 굳이 thread 간의 동시적인 처리가 필요하다면 다른 Python implementation을 고려해봐도 된다. Jython, IronPython, Stackless Python, PyPy 등이 있다.
장점
- 레퍼런스 카운팅을 사용하는 메모리 관리 방식에서 GIL 덕분에 오버헤드가 적어 싱글 스레드일 때 fine grained lock 방식보다 성능이 우월
- 구현이 쉼다
- C extension 을 활용할 때 GIL 은 해제되므로 C library 를 사용하는 CPU bound 프로그램을 멀티 스레드로 실행하는 경우 더 빠를 수 있다
Without GIL
Optional in Python 3.13
GIL Become Optional in Python 3.13
GIL or Global Interpreter Lock can be disabled in Python version 3.13. This is currently experimental.
https://geekpython.in/gil-become-optional-in-python

A GIL-less Future for Python Beckons Developers
The GIL debate led to internal discussions, resulting in PEP 703's acceptance to remove it. Python's future looks bright without the GIL constraint.
https://analyticsindiamag.com/a-gil-less-future-for-python-beckons-developers

PEP 703 (Making the Global Interpreter Lock Optional in CPython) acceptance
(Posted for the whole Steering Council.) As we’ve announced before, the Steering Council has decided to accept PEP 703 (Making the Global Interpreter Lock Optional in CPython) . We want to make it clear why, and under what expectations we’re doing so. It is clear to the Steering Council that theoretically, a no-GIL (or free-threaded) Python would be of great benefit, and the majority of the community seems in agreement. Threads have significant downsides and caveats, but they are widely adopte...
https://discuss.python.org/t/pep-703-making-the-global-interpreter-lock-optional-in-cpython-acceptance/37075

Why
왜 Python에는 GIL이 있는가
Python 사용자라면 한 번 쯤은 들어봤을 (안 들어봤다 해도 괜찮아요) 악명 높은 GIL (Global Interpreter Lock)에 대해 정리해본다. 그래서 GIL은 무엇인가? Python Wiki에서는 이렇게 말한다. In CPython, the global interpreter lock, or GIL, is a mutex that protects access to Python objects, preventing multiple threads from executing Python bytecodes at once.
https://dgkim5360.tistory.com/entry/understanding-the-global-interpreter-lock-of-cpython
Thread Safe
python gil이 있으니까 thread safe 할까요?
multithreading을 공부하시다 보면, GIL에 대해 한 번 정도는 들어보시게 됩니다. Global Interpreter Lock의 줄임말입니다. 그런데 이것이 있으면 Lock이 필요 없을까요? 을 봐도, 답은 한결같이 아닐 수도 있다는 것이였습니다. 를 읽어 보시면, 각각의 byte code가 어쩌고 저쩌고 언급을 하다가 atomic 하다는 언급 을 하고 있어요. 아래 예제를 보도록 하겠습니다.
https://codingdog.tistory.com/entry/python-gil%EC%9D%B4-%EC%9E%88%EC%9C%BC%EB%8B%88%EA%B9%8C-thread-safe-%ED%95%A0%EA%B9%8C%EC%9A%94
Interview_Question_for_Beginner/Python at master · JaeYeopHan/Interview_Question_for_Beginner
Generator(제네레이터)는 제네레이터 함수가 호출될 때 반환되는 iterator(이터레이터)의 일종이다. 제네레이터 함수는 일반적인 함수와 비슷하게 생겼지만 yield 구문 을 사용해 데이터를 원하는 시점에 반환하고 처리를 다시 시작할 수 있다. 일반적인 함수는 진입점이 하나라면 제네레이터는 진입점이 여러개라고 생각할 수 있다. 이러한 특성때문에 제네레이터를 사용하면 원하는 시점에 원하는 데이터를 받을 수 있게된다.
https://github.com/JaeYeopHan/Interview_Question_for_Beginner/tree/master/Python#celery

Seonglae Cho

