Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Every Python object has a backing dictionary, so if any object has to be shared between threads in a threadsafe fashion, then dictionaries need to be threadsafe.


This is tricky because dictionaries need to call __hash__ which can run arbitrary Python code, so it's non-trivial to make the dictionary threadsafe.


It's perfectly possible and even reasonable to call __hash__ before acquiring lock on the dictionary (and cache keys' hashes for data in the dictionary).

On the other hand, when almost every object is backed by dictionary, the locking has to be very fast, which seems to me like almost unsolvable problem.


__eq__ as well


Just make __hash__() wrap the underlying dict in a thread-safe one that delegates the calls.


Right. I think that backing dictionary could probably be replaced with a more thread-friendly implementation, even if we have to offer reduced guarantees.


Or every Python object's backing dictionary needs to use the special threadsafe implementation.


That's an argument for revamping the backing dictionary, really. If __hash__() is a problem, make it return a thread-safe proxy that delegates to the object's dictionary.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: