0

I am writing a Python module in which two threads access one list. One thread adds 500 items to the list per second, and the other thread reads the list at an irregular interval. I want to make a thread-safe "list" class to avoid having to use locks every time I read or write to the list (suggested by this answer to a previous question on SO).

Here is my first go at a thread-safe list class (with help from these previous SO answers: 1 and 2). Are there any methods that should be locked that are not currently locked, or any methods that do not require a lock that are currently locked?

import collections
import threading

class ThreadSafeList(collections.MutableSequence):
    """Thread-safe list class."""
    def __init__(self, iterable=None):
        if iterable is None:
            self._list = list()
        else:
            self._list = list(iterable)
        self.rlock = threading.RLock()

    def __len__(self): return len(self._list)

    def __str__(self): return self.__repr__()

    def __repr__(self): return "{}".format(self._list)

    def __getitem__(self, i): return self._list[i]

    def __setitem__(self, index, value):
        with self.rlock:
            self._list[index] = value

    def __delitem__(self, i): 
        with self.rlock:
            del self._list[i]

    def __iter__(self):
        with self.rlock:
            for elem in self._list:
                yield elem

    def insert(self, index, value): 
        with self.rlock:
            self._list.insert(index, value)
Community
  • 1
  • 1
jkr
  • 17,119
  • 2
  • 42
  • 68
  • This might be better suited to [codereview.se]. – Peter Wood Jan 18 '17 at 15:11
  • I wouldn't be shocked if `__getitem__` needed to be locked due to accessing an index, though I wasn't able to find the source code for the Python list internal to check. – Alex W Jan 18 '17 at 15:13
  • 1
    Based on the [source code](https://svn.python.org/projects/python/trunk/Objects/listobject.c) it looks like `PyList_GetItem` uses `ob_item[i]` so you would want to lock that, as there is a note in the comments that says "Note that self->ob_item may change, and even if newsize is less than ob_size on entry". – Alex W Jan 18 '17 at 15:35

0 Answers0