python-sortedcontainers
python-sortedcontainers copied to clipboard
Support thread-safety
Dear Author(s), it would be nice to have a thread-safe version of sorted containers.
It would be nice but I don't see a convenient way forward. Couple strategies come to mind:
- Implement a C-extension for CPython with an eye for thread-safety in doing so. The deque data type in collections is implemented this way. Because CPython has a GIL, C-implemented functions often end up thread-safe by default. Of course, this would render sortedcontainers incompatible with PyPy, Jython, and other Python implementations :(
- Guard every data type method with some set of locks, possibly with separate ones for read-only and read-write operations. I am open to providing this functionality but it would be require funding and potentially be expensive.
Neither of those is trivial to implement and each feels a bit beyond the current project scope. Did you have an idea as to how to implement thread safety?
There may be a clever metaclass you could write which would implement coarse-grained locking around each method. Could you describe your use case? Maybe you could queue reads/writes using collections.deque and place one thread in charge of the sorted container.
If it helps for when someone implements this, reduce is not thread safe. (needs a lock preventing deletes)
File "/usr/local/lib/python2.7/dist-packages/sortedcontainers/sorteddict.py", line 575, in __reduce__
return (self.__class__, (self._key, list(self.items())))
File "/usr/lib/python2.7/_abcoll.py", line 475, in __iter__
yield (key, self._mapping[key])
KeyError: Decimal('0.0000708300000000')
Won’t fix.