threading appears to run threads sequentially

后端 未结 4 1402
醉话见心
醉话见心 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:24

    The behaviour may also change depending on if the system is using has a single processor or multiple processors, as explained by this talk by David Beazley.

    As viraptor says, the first thread will release the GIL after executing sys.getcheckinterval() bytecodes (100 by default). To crudly summarise what David Beazley says, on a single processor system the second thread will then have a chance to take over. However on a multi-core system the second thread may be running on a different core, and the first thread will try to reacquire the lock and will probably succeed since the OS will not have had time to switch processors. This means that on a multi-core system with a CPU-bound thread the other threads may never get a look in.

    The way round this is to add a sleep statement to both of the loops so that they are no longer CPU bound.

提交回复
热议问题