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)