28 |
# NOTES: |
# NOTES: |
29 |
# |
# |
30 |
|
|
31 |
import sha, time |
import sha, time, thread |
32 |
|
|
33 |
class InvalidObjectReference(StandardError): |
class InvalidObjectReference(StandardError): |
34 |
pass |
pass |
47 |
_index += time.clock() |
_index += time.clock() |
48 |
object._ObjectLibrarian__refid = \ |
object._ObjectLibrarian__refid = \ |
49 |
sha.new("%s%s%s"%(id(object), object, _index)).hexdigest() |
sha.new("%s%s%s"%(id(object), object, _index)).hexdigest() |
50 |
|
|
51 |
|
# add creation time |
52 |
|
now = time.time() |
53 |
|
object._ObjectLibrarian__creationtime = now |
54 |
|
object._ObjectLibrarian__lastusage = now |
55 |
|
|
56 |
return object._ObjectLibrarian__refid |
return object._ObjectLibrarian__refid |
57 |
|
|
58 |
|
|
62 |
|
|
63 |
def retrieveObject(handle): |
def retrieveObject(handle): |
64 |
try: |
try: |
65 |
return _objects[handle] |
object = _objects[handle] |
66 |
|
|
67 |
|
if hasattr(object,"_ObjectLibrarian__myGC"): |
68 |
|
# call private Garbage Collector (f.e. for sessions) |
69 |
|
object._ObjectLibrarian__myGC.beingRetrieved(object) |
70 |
|
else: |
71 |
|
# just store last used time |
72 |
|
object._ObjectLibrarian__lastusage = time.time() |
73 |
|
|
74 |
|
return object |
75 |
except KeyError: |
except KeyError: |
76 |
raise InvalidObjectReference |
raise InvalidObjectReference |
77 |
|
|
83 |
pass |
pass |
84 |
except AttributeError: |
except AttributeError: |
85 |
pass |
pass |
86 |
|
|
87 |
|
def getObjectLibrarianStatus(): |
88 |
|
status={} |
89 |
|
status["objcount"]=len(_objects) |
90 |
|
return status |
91 |
|
|
92 |
|
class GarbageCollector: |
93 |
|
def __init__(self): |
94 |
|
self.defaultTimeout=3600 # in seconds |
95 |
|
self.loopLength=20 # in seconds |
96 |
|
self.running=0 |
97 |
|
|
98 |
|
def setDefaultTimeout(self,timespan): |
99 |
|
self.defaultTimeout=timespan |
100 |
|
|
101 |
|
def getDefaultTimeout(self): |
102 |
|
return self.defaultTimeout |
103 |
|
|
104 |
|
def setLoopLength(self,length): |
105 |
|
self.loopLength=length |
106 |
|
|
107 |
|
def getLoopLength(self): |
108 |
|
return self.loopLength |
109 |
|
|
110 |
|
def cleanUp(self): |
111 |
|
# is this threadsafe ? |
112 |
|
try: |
113 |
|
keys=_objects.keys() |
114 |
|
now=time.time() |
115 |
|
for handle in keys: |
116 |
|
try: |
117 |
|
object=_objects[handle] |
118 |
|
if not hasattr(object,"_ObjectLibrarian__myGC"): |
119 |
|
if object._ObjectLibrarian__lastusage + self.defaultTimeout < now: |
120 |
|
deferenceObject(object) |
121 |
|
GDebug.printMsg(4,"Timeout reached for object %s" & object) |
122 |
|
|
123 |
|
except: |
124 |
|
pass |
125 |
|
|
126 |
|
except: |
127 |
|
pass |
128 |
|
|
129 |
|
def loop(self): |
130 |
|
while 1==1: |
131 |
|
try: |
132 |
|
self.cleanUp() |
133 |
|
time.sleep(self.loopLength) |
134 |
|
except: |
135 |
|
pass |
136 |
|
|
137 |
|
def start_in_new_thread(self): |
138 |
|
if self.running: |
139 |
|
return |
140 |
|
self.running=1 |
141 |
|
thread.start_new_thread(self.loop,()) |
142 |
|
|
143 |
|
|
144 |
|
_global_GarbageCollector = GarbageCollector() |
145 |
|
|
146 |
|
|