python dictionary is thread safe?

后端 未结 4 1728
小鲜肉
小鲜肉 2020-12-01 10:56

Some stated that python dictionary is thread safe. Does it mean I can or cannot modify the items in a dictionary while iterating over it?

4条回答
  •  萌比男神i
    2020-12-01 11:22

    The two concepts are completely different. Thread safety means that two threads cannot modify the same object at the same time, thereby leaving the system in an inconsistent state.

    That said, you cannot modify a dictionary while iterating over it. See the documentation..

    The dictionary p should not be mutated during iteration. It is safe (since Python 2.1) to modify the values of the keys as you iterate over the dictionary, but only so long as the set of keys does not change.

提交回复
热议问题