Search code examples
pythonpython-3.xmultithreadinglockingpython-multithreading

If GIL is there, what is the use of locks in multithreading environment in python?


Multithreading in python has a lock provided. One can access it using from threading import lock

I understand that because of GIL in python only one thread can use the python interpreter. Now if that is the case can somebody please help me understand what could possibly be the use of this lock exposed by the threading library.

I would appreciate it if you can explain with an example


Solution

  • I belive you still need ordering, and waiting for other threads. Python still creates many threads they just run one at the time. If you need some synchronization that where locks come into play. The lock is also used by context managers.