import sys import time import copy import threading from threading import Thread _TESTING = False #Define a thread safe object storage type class cSyncObject(object): ''' constructor ''' def __init__(self, name="", value=None, maxage=10): self.name = name self.maxage = maxage self.__dataLock = threading.RLock() self.__stamp = time.time() self.__value = value self.__isinit = False @property def CURRENTVALUE(self): val = None try: self.__dataLock.acquire() val = copy.deepcopy(self.__value) finally: self.__dataLock.release() return (val) @property def ISVALID(self): timeElapsed = None try: self.__dataLock.acquire() timeElapsed = time.time() - self.__stamp finally: self.__dataLock.release() return (timeElapsed < self.maxage) @property def ISINIT(self): try: self.__dataLock.acquire() isInit = self.__isinit finally: self.__dataLock.release() return (isInit) @property def VAL(self): val = None try: self.__dataLock.acquire() if _TESTING: print '@Reading: ', self.name, self.__value val = copy.deepcopy(self.__value) if _TESTING: print '@End read: ', self.name, val finally: self.__dataLock.release() return (val) @property def VALUE(self): val = None try: self.__dataLock.acquire() if _TESTING: print '@Reading: ', self.name, self.__value timeElapsed = time.time() - self.__stamp #-------------No Maxage with async. Dbus signals------- if ((timeElapsed > self.maxage or self.__value == None) and self.maxage != -1): return (val) #endif val = copy.deepcopy(self.__value) if _TESTING: print '@End read: ', self.name, val finally: self.__dataLock.release() return (val) @VALUE.setter def VALUE(self, value): try: self.__dataLock.acquire() if _TESTING: print '@Set value:', self.name, self.__value, 'to:', value self.__value = value self.__isinit = True self.__stamp = time.time() if _TESTING: print '@End Set: ', self.name, self.__value finally: self.__dataLock.release() @property def AGE(self): timeElapsed = None try: self.__dataLock.acquire() timeElapsed = time.time() - self.__stamp finally: self.__dataLock.release() return (timeElapsed) #End of class cSyncObject #---------------- Test ------------------ if __name__ == '__main__': _TESTING = True v = cSyncObject("karim", 123) print 'init value is: ', v.VALUE print 'is it init:', v.ISINIT v.VALUE = None print 'reset value is: ', v.CURRENTVALUE print 'is it init:', v.ISINIT r = 225 v.VALUE = r print 'new value is: ', v.VALUE, 'r= ', r r = 8999 print 'next value is: ', v.VALUE, 'r= ', r print 'Age is: ' , v.AGE, 'seconds' time.sleep(2) print 'Age is: ' , v.AGE, 'seconds' time.sleep(2) print 'Age is: ' , v.AGE, 'seconds' time.sleep(2) print 'Age is: ' , v.AGE, 'seconds' time.sleep(5) print 'IsValid: ', v.ISVALID, 'Age is: ', v.AGE, 'seconds', 'MaxAge is: ' , v.maxage, 'seconds' print 'value is: ', v.VALUE, ' current is: ', v.CURRENTVALUE # thread test shutdown = False v.VALUE = 0 # def f(id): while (not shutdown): #time.sleep(id) #print 'Thrd(', id,'): Reading\n' val = v.VALUE #print 'Thrd(', id,'): Writing\n' v.VALUE = val + id thrd1 = Thread(target=f, args=(3,)) thrd1.start() thrd2 = Thread(target=f, args=(1,)) thrd2.start() ret = raw_input("press any key to exit:") shutdown = True thrd1.join(10.0) thrd2.join(10.0) if thrd1.is_alive(): print 'thrd1 is running' if thrd2.is_alive(): print 'thrd2 is running' else: print 'exited'