1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
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
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
74
75
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
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
95 """
96 This method must be used in together with the method synchronizeOn().
97 """
98 self.__semaphore.release()
99
100
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
109 return iValue
110
113
114
116 """
117 Initializes the timer for the object
118 """
119 if self.__fTimeInit is None:
120 self.__fTimeInit = time.time()
121
122
123
124
126 """
127 Retrieves the time of initialization
128 @return: number of seconds since epoch
129 @rtype: float
130 """
131 return self.__fTimeInit
132
133
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
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
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