1

I want to create a class(say, LockedAttributes) to access(READ/WRITE) some attributes safely by multiple threads.I want to pass those attributes that I want to share as a list to the LockedAttributes class. Some of the list elements are itself class objects with it's own setter and getter. How can i access those setter/getter from a LockedAttribute class obj? My use of getattr() setattr() might be wrong. sample code:

class Coord:

def __init__(self, x=0.0, y=0.0, z=0.0):
    self.x = x
    self.y = y
    self.z = z

def set_coordinator(self, x, y, z):
    self.x = x
    self.y = y
    self.z = z

def get_coordinator(self):
    return self.x, self.y, self.z

class LockedAttributes(object):
def __init__(self, obj):
    self.__obj = obj
    self.__lock = RLock()

def getmyPosition(self):
    with self.__lock:
        return self.__obj[0]

def getmySpeed(self):
    with self.__lock:
        return self.__obj[1]

def getcolPosition(self):
    with self.__lock:
        return self.__obj[2]
def getDistfromCol(self):
    with self.__lock:
        getattr(self, self.__obj[3]) 
def setDistfromCol(self, value):
    with self.__lock:
        setattr(self, self.__obj[3], value) 
def getcolactivationFlag(self):
    with self.__lock:
        getattr(self, self.__obj[4])

def setcolactivationFlag(self, value):
    with self.__lock:
        setattr(self, self.__obj[3], value)


class OBU():
def __init__(self):     
   pos = Coord()
  speed = Coord()
  colpos = Coord()
  distance_from_accident = 0.0
  Flag = False
  self.shared_attributes = LockedAttributes([ pos, speed, colpos, distance_from_accident, Flag])

  mypos= self.shared_attributes.getmyPosition()
  mypos.get_coordinator() # Not workinh
GreenRoad-Dk
  • 57
  • 1
  • 9

1 Answers1

0

The __init__ method of the LockedAttributes class should take an argument so that you can actually pass a list object to it.

Change:

class LockedAttributes(object):
    def __init__(self):
        self.__obj = object
        self.__lock = RLock()

To:

class LockedAttributes(object):
    def __init__(self, obj):
        self.__obj = obj
        self.__lock = RLock()
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • I have edited my code. but the problem still persists.Seems like getmyPosition() is not returning the Coord() object and I can't use the setter/getter. One question, even though when I would be able to access the position object through getmyPosition in a safe way, I am going to READ/WRITE it's value using Coord class's own methods. Would that be thread safe as well? – GreenRoad-Dk Feb 27 '19 at 18:52
  • 1
    Yes it would be thread-safe to do that because the lock would prevent other threads from entering the getters/setters at the same time, thereby preventing them from further entering methods of `Coord`. – blhsing Feb 27 '19 at 19:14
  • Thank you! But, getmyPosition() is not returning the first element of the list. Which is the pos object. Do you have any idea why? – GreenRoad-Dk Feb 27 '19 at 19:25
  • 1
    It definitely should be working. Please run the demo: https://repl.it/repls/WelltodoMotherlyProcesses – blhsing Feb 27 '19 at 19:38
  • YES. it is working. I am using with lock: Don't I need to release the lock? – GreenRoad-Dk Feb 27 '19 at 23:37
  • 1
    Good to know. By using the lock as a context manager with the `with` statement, it will release the lock for you when it leaves the `with` block so you don't have to worry about manually releasing it. – blhsing Feb 28 '19 at 00:12
  • Apparently, what is happening, 'with lock:' is returning mypos object. I am using class methods on that object. But it's not returning to the with lock: block. – GreenRoad-Dk Feb 28 '19 at 00:51