threading appears to run threads sequentially

后端 未结 4 1406
醉话见心
醉话见心 2020-12-03 17:30

I am trying to use threads in a Python project I am working on, but threads don\'t appear to be behaving as they are supposed to in my code. It seems that all threads run se

4条回答
  •  我在风中等你
    2020-12-03 18:14

    Currently in python, threads get changed after executing some specified amount of bytecode instructions. They don't run at the same time. You will only have threads executing in parallel when one of them calls some I/O-intensive or not python-affecting module that can release GIL (global interpreter lock).

    I'm pretty sure you will get the output mixed up if you bump the number of loops to something like 10000. Remember that simply spawning the second thread also takes "a lot" of time.

提交回复
热议问题