Module EDObject
[hide private]
[frames] | no frames]

Source Code for Module EDObject

  1  # coding: utf8 
  2  # 
  3  #    Project: The EDNA Kernel 
  4  #             http://www.edna-site.org 
  5  # 
  6  #    File: "$Id: EDImportLib.py 1453 2010-04-28 16:20:46Z svensson $" 
  7  # 
  8  #    Copyright (C) 2010-2010 European Synchrotron Radiation Facility 
  9  #                            Grenoble, France 
 10  # 
 11  #    Principal authors:  
 12  #                       Olof Svensson (svensson@esrf.fr)  
 13  #                       Jérôme Kieffer (jerome.kieffer@esrf.fr) 
 14  # 
 15  #    This program is free software: you can redistribute it and/or modify 
 16  #    it under the terms of the GNU Lesser General Public License as published 
 17  #    by the Free Software Foundation, either version 3 of the License, or 
 18  #    (at your option) any later version. 
 19  # 
 20  #    This program is distributed in the hope that it will be useful, 
 21  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 22  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 23  #    GNU Lesser General Public License for more details. 
 24  # 
 25  #    You should have received a copy of the GNU General Public License 
 26  #    and the GNU Lesser General Public License  along with this program.   
 27  #    If not, see <http://www.gnu.org/licenses/>. 
 28  # 
 29   
 30  from __future__ import with_statement 
 31   
 32  """ 
 33  EDObject is the core of all EDNA objects,  
 34  it offers some simple but efficient synchronization scheme based on Semaphores  
 35  it offers timing facilities (uninitialized by default)  
 36  """ 
 37   
 38  __authors__ = ["Olof Svensson", "Jérôme Kieffer"] 
 39  __contact__ = "svensson@esrf.eu" 
 40  __license__ = "LGPLv3+" 
 41  __copyright__ = "European Synchrotron Radiation Facility, Grenoble, France" 
 42   
 43   
 44  import time 
 45  from EDThreading import Semaphore 
 46   
47 -class EDObject(object):
48 """ 49 Virtual base class for all EDNA Objects (classes). 50 It offers some synchronization and locking capabilities to make the code thread safe. 51 """ 52 __semaphoreId = Semaphore() 53 __iId_class = 0 54
55 - def __init__(self):
56 """ 57 Constructor of the main pure virtual class. 58 This constructor implements: 59 - the creation of the semaphore 60 - definition of timer object (uninitialized as potentially not used) 61 """ 62 object.__init__(self) 63 with self.__class__.__semaphoreId: 64 self.__class__.__iId_class += 1 65 self.__iId = self.__class__.__iId_class 66 self.__semaphore = Semaphore() 67 self.__fTimeInit = None 68 self.__fTimeEnd = None 69 self.__classname = None
70 71
72 - def getId(self):
73 return self.__iId
74 75
76 - def getClassName(self):
77 """ 78 Retrieves the name of the class 79 @return: the name of the class 80 @rtype: string 81 """ 82 return self.__class__.__name__
83 84
85 - def synchronizeOn(self):
86 """ 87 This method must be used in together with the method synchronizeOff(). 88 This method makes the code threadsafe till the method synchronizeOff 89 is called. 90 """ 91 self.__semaphore.acquire()
92 93
94 - def synchronizeOff(self):
95 """ 96 This method must be used in together with the method synchronizeOn(). 97 """ 98 self.__semaphore.release()
99 100
101 - def getSemaphoreValue(self):
102 """ 103 This method should only be used for debugging purpose... 104 @return: the "internal" value of the semaphore 105 @rtype: integer 106 """ 107 iValue = self.__semaphore._Semaphore__value 108 #EDVerbose.WARNING("DEBUG INFO: The value of semaphore for instance of class %s with hash %s is %i" % (self.getClassName(), hash(self), iValue)) 109 return iValue
110
111 - def locked(self):
112 return self.__semaphore
113 114
115 - def setTimeInit(self):
116 """ 117 Initializes the timer for the object 118 """ 119 if self.__fTimeInit is None: 120 self.__fTimeInit = time.time()
121 122 123 124
125 - def getTimeInit(self):
126 """ 127 Retrieves the time of initialization 128 @return: number of seconds since epoch 129 @rtype: float 130 """ 131 return self.__fTimeInit
132 133
134 - def setTimeEnd(self):
135 """ 136 Set the end of calculation time for the given object 137 """ 138 if self.__fTimeEnd is None: 139 self.__fTimeEnd = time.time()
140 141
142 - def getTimeEnd(self):
143 """ 144 Retrieves the time of end of task 145 @return: number of seconds since epoch 146 @rtype: float 147 """ 148 return self.__fTimeEnd
149
150 - def getRunTime(self):
151 """ 152 @returns: the RunTime for the given object 153 @rtype: float 154 """ 155 fRetrunRunTime = 0.0 156 if self.__fTimeInit is not None: 157 if self.__fTimeEnd is None: 158 fRetrunRunTime = time.time() - self.__fTimeInit 159 else: 160 fRetrunRunTime = self.__fTimeEnd - self.__fTimeInit 161 return fRetrunRunTime
162