2008/9/2 Swapnil.ST@gmail.com
I am working on an application which embeds python in the application threads and each thread can further create threads using the python thread module few things i don't understand:
- why does the interpreter ie the main thread keeps releasing and
reacquiring the GIL every few instruction. why isn't GIL simply allowed to be acquired by any thread which requires it then. I mean why is the acquisition and release of a lock kept periodic and not on need basis?( which is obviously the way it should be)
Because Python _always_ needs the lock as long as there is Python code left to execute. If it wasn't periodically released and re-acquired, other threads that want to execute Python code would forever block.
- The purpose of GIL was not very clear to me from python-docs. is it just
a single lock to guard every global variable? or is it something more?
Anything that needs to call a Python API needs to acquire this lock first. GIL == Global Interpreter Lock. So it really guards access to the whole Python subsystem.
- suppose I am not making any blocking call then I would never release GIL
since that is the only thing guarding my thread state and i don't want my thread state messed up any time my thread is running. Which implies unacceptable starvation of other threads.
You don't release the GIL but Python will release it periodically, as you mentioned above. If you need more protection, you need to use your own locks (threading.Lock).