开发者

Python threading and GIL

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 sequentiall开发者_开发问答y, 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.


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.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜