Python threading and GIL

被刻印的时光 ゝ 提交于 2019-11-30 05:17:18

问题


I was reading about the GIL and it never really specified if this includes the main thread or not (i assume so). Reason I ask is because I have a program with threads setup that modify a dictionary. The main thread adds/deletes based on player input while a thread loops the data updating and changing data.

However in some cases a thread may iterate over the dictionary keys where one could delete them. If there is a so called GIL and they are run sequentially, why am I getting dict changed errors? If only one is suppose to run at a time, then technically this should not happen.

Can anyone shed some light on such a thing? Thank you.


回答1:


They are running at the same time, they just don't execute at the same time. The iterations might be interleaved. Quote Python:

The mechanism used by the CPython interpreter to assure that only one thread executes Python bytecode at a time.

So two for loops might run at the same time, there will just be no (for example) two del dict[index]'s at the same time.




回答2:


The GIL locks at a Python byte-code level, and applies to all threads, even the main thread. If you have one thread modifying a dictionary, and another iterating keys, they will interfere with each other.

"Only one runs at a time" is true, but you have to understand the unit of granularity. In the case of CPython's GIL, the granularity is a bytecode instruction, so execution can switch between threads at any bytecode.




回答3:


The gil prevents two threads from modifying the interpreter state simultaneously. It doesn't provide any thread consistency constraints, or any kind of mutex at all on a granularity smaller than the whole process. If you need to read and modify a dict in two threads, you should be using a mutex




回答4:


Python switches threads more often than you seem to think it does. You say "only one" is supposed to run at a time, and technically that's true, but it depends on your definition of "one." Python's atomic operations are very small. For example: adding a single item to a dictionary. Iteration over an entire dictionary can be interrupted.

You should use a lock object from the threading library to isolate your program's atomic operations.



来源:https://stackoverflow.com/questions/4748787/python-threading-and-gil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!