python dictionary is thread safe?

后端 未结 4 1738
小鲜肉
小鲜肉 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条回答
  •  忘掉有多难
    2020-12-01 11:17

    You cannot iterate over a dictionary if you add or delete elements in the another thread concurrently. You can get "RuntimeError: dictionary changed size during iteration" or "KeyError" errors.

    See live example and you can play with it.

    And yes, you can iterate, change values of elements, get elements of it without exceptions in different threads concurrently.

提交回复
热议问题