python-sortedcontainers icon indicating copy to clipboard operation
python-sortedcontainers copied to clipboard

Support thread-safety

Open ghost opened this issue 7 years ago • 2 comments
trafficstars

Dear Author(s), it would be nice to have a thread-safe version of sorted containers.

ghost avatar Sep 18 '18 11:09 ghost

It would be nice but I don't see a convenient way forward. Couple strategies come to mind:

  1. 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 :(
  2. 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.

grantjenks avatar Sep 25 '18 22:09 grantjenks

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')

scottbecker avatar Oct 19 '18 23:10 scottbecker

Won’t fix.

grantjenks avatar Nov 29 '22 04:11 grantjenks