Implementing a simple network data structure in Python and race conditions

Let's consider a starting implementation of this data structure in Python. Navigate to the Chapter16/network.py file, as follows:

# Chapter16/network.pyimport timefrom random import choiceclass Network:    def __init__(self, primary_key, primary_value):        self.primary_key = primary_key        self.data = {primary_key: primary_value}    def __str__(self):        result = '{\n'        for key in self.data:            result += f'\t{key}: {self.data[key]};\n'        return result + '}'    def add_node(self, key, value):        if key not in self.data:            self.data[key] = value            return True        return False        # precondition: the object has more than one node left    def refresh_primary(self):        del self.data[self.primary_key] self.primary_key ...

Get Mastering Concurrency in Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.