简体   繁体   English

Python相当于来自Java的concurrentHashMap?

[英]Python equivalent of concurrentHashMap from Java?

I know that dictionaries are atomic in python but (correct me if I'm wrong) that means only a single addition to the dictionary can be completed at a time. 我知道字典在python中是原子的,但是(如果我错了,请纠正我)这意味着一次只能完成对字典的一次添加。 According to the Java page for the concurrentHashMap : "The table is internally partitioned to try to permit the indicated number of concurrent updates without contention." 根据concurrentHashMap的Java页面:“该表在内部进行分区,以尝试允许指定数量的并发更新而不会发生争用。” Wouldn't solely atomic insertion in python not compare in speed to the Java implementation 不会单独在python中进行原子插入,而不是在速度上与Java实现相比

EDIT: When I wrote "that means only a single addition to the dictionary can be completed at a time," I meant to say that the state of the dictionary is going to be discretized based on the individual dictionary addition 编辑:当我写“这意味着一次只能完成一个字典的添加,”我的意思是说,字典的状态将根据个别字典添加进行离散化

In Python, due to the Global Interpreter Lock (GIL), a process can only execute one python bytecode at a time, regardless of how many threads it has. 在Python中,由于全局解释器锁(GIL),一个进程一次只能执行一个python字节码,无论它有多少线程。 This means inserting/updating/reading a key to a dictionary is thread-safe, which is what people usually mean by saying a dictionary's get/put are "atomic". 这意味着插入/更新/读取字典中的键是线程安全的,这通常是人们通常所说的字典的get / put是“原子”。

But this means that, exactly as you suspected, multiple threads trying to update different keys to the same dictionary will not be concurrent. 但这意味着,正如您所怀疑的那样,尝试将不同密钥更新到同一字典的多个线程将不会并发。 Java, of course, doesn't have the GIL issue so multiple threads can update different keys in the ConcurrentHashMap at the same time. 当然,Java没有GIL问题,因此多个线程可以同时更新ConcurrentHashMap中的不同键。 This doesn't always happen; 这并不总是发生; it's just possible. 这是可能的。 The ConcurrentHashMap implementation shards the set of keys and locks each shard. ConcurrentHashMap实现分片键集并锁定每个分片。 Each shard can be read concurrently but only one thread can write at a time. 每个分片可以同时读取,但一次只能写入一个线程。


†: Sometimes it's pointed out objects with a __hash__ method written in Python will require multiple Python bytecodes, so then puts and gets are not atomic per se; †:有时指出用Python编写的__hash__方法的对象需要多个Python字节码,因此__hash__和gets本身不是原子的; however simple puts and gets will still be thread-safe in the sense that they will not cause crashes or garbage values, although you can still have race-conditions. 然而,简单的puts和gets仍然是线程安全的,因为它们不会导致崩溃或垃圾值,尽管你仍然可以有竞争条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM