Are locks unnecessary in multi-threaded Python code because of the GIL?

前端 未结 9 1098
遥遥无期
遥遥无期 2020-12-02 08:11

If you are relying on an implementation of Python that has a Global Interpreter Lock (i.e. CPython) and writing multithreaded code, do you really need locks at all?

9条回答
  •  鱼传尺愫
    2020-12-02 08:43

    You still need to use locks (your code could be interrupted at any time to execute another thread and this can cause data inconsistencies). The problem with GIL is that it prevents Python code from using more cores at the same time (or multiple processors if they are available).

提交回复
热议问题