python字典是线程安全的吗? [英] python dictionary is thread safe?

查看:42
本文介绍了python字典是线程安全的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人说python字典是线程安全的.这是否意味着我可以或不能在迭代时修改字典中的项目?

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?

推荐答案

这两个概念是完全不同的.线程安全意味着两个线程不能同时修改同一个对象,从而使系统处于不一致的状态.

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..

字典 p 在迭代过程中不应该发生变异.它是安全的(自 Python 2.1 起)在迭代字典时修改键的值,但只要密钥集不会改变.

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.

这篇关于python字典是线程安全的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆