Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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." Wouldn't solely atomic insertion in python not compare in speed to the Java implementation

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

like image 886
Fancypants753 Avatar asked Jan 06 '18 04:01

Fancypants753


1 Answers

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

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. This doesn't always happen; it's just possible. The ConcurrentHashMap implementation shards the set of keys and locks each shard. 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; 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.

like image 136
C S Avatar answered Sep 19 '22 11:09

C S