Why do we need locks for threads, if we have GIL?

前端 未结 4 1743
心在旅途
心在旅途 2021-01-31 15:53

I believe it is a stupid question but I still can\'t find it. Actually it\'s better to separate it into two questions:

1) Am I right that we could have a lot of threads

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-31 16:44

    the GIL prevents simultaneous execution of multiple threads, but not in all situations.

    The GIL is temporarily released during I/O operations executed by threads. That means, multiple threads can run at the same time. That's one reason you still need locks.

    I don't know where I found this reference.... in a video or something - hard to look it up, but you can investigate further yourself

    UPDATE:

    The few thumbs down I got signal to me that people think memory is not a good enough reference, and google not a good enough database. While I'd disagree with that, let me provide one of the first URLs I looked up (and checked!), so the people who disliked my answer can live happily from how on: https://wiki.python.org/moin/GlobalInterpreterLock

提交回复
热议问题