[Python] How to make built-in containers (sets, dicts, lists) thread safe?

Discussão em 'Python' iniciado por Stack, Junho 7, 2025.

  1. Stack

    Stack Membro Participativo

    I understand from this question that if I want to have a set which is thread-safe I have to implement the thread-safety part on my own.

    Therefore I could come up with:

    from threading import Lock

    class LockedSet(set):
    """A set where add() and remove() are thread-safe"""

    def __init__(self, *args, **kwargs):
    # Create a lock
    self._lock = Lock()
    # Call the original __init__
    super(LockedSet, self).__init__(*args, **kwargs)

    def add(self, elem):
    self._lock.acquire()
    try:
    super(LockedSet, self).add(elem)
    finally:
    self._lock.release()

    def remove(self, elem):
    self._lock.acquire()
    try:
    super(LockedSet, self).remove(elem)
    finally:
    self._lock.release()


    So, of course only add() and remove() are thread-safe in this implementation. The other methods are not because they were not overwritten in the subclass.

    Now, the pattern is pretty simple: acquire lock, call original method, release lock. If I follow the logic above, I would have to overwrite all methods exposed by set in essentially the same way, e.g.:

    (pseudo-code)

    def <method>(<args>):
    1. acquire lock
    2. try:
    3. call original method passing <args>
    4. finally:
    5. release lock


    (/pseudo-code)

    This is not only tedious but also prone to errors. So, any ideas/suggestions on how to approach this in a better way?

    Continue reading...

Compartilhe esta Página