Python GIL(Global Interpreter Lock)

作者:半瓶墨水   链接:

*Why We Need GIL*

1. GIL enforces no parallel python binary code running in same process

2. This is good for C library extension
— means the library will not been running in parallel in 2 python threads in one process
— make quite a few things more simpler(need to use event/lock otherwise)
— even if you “don’t write C extension”, a lot of libraries are implemented in C

3. python Garbage Collection is reference counting + cycle-reference removal
— it depend on GIL in order to be consistent

*When does GIL behaves BADLY*

1. It can’t leverage multi-core CPUs when doing CPU extensive tasks
— this can be mitigated by using C extension libs like NumPy
— if you are doing IO (network/file, etc) in between, then it won’t be a huge problem

2. it sometimes blocks Ctrl+C on command line, you had to kill -9 sometimes