22 from threading
import Event
23 from thread
import start_new_thread
24 from numpy.oldnumeric
import sum
27 from AbstractGrid
import TimeoutError
34 MSG_CLIENT_CRASHED = 2
35 MSG_CALL_METHOD_RESULT = 15
51 class MyResult(object):
57 self.__dict__[
'event'] = Event()
58 self.__dict__[
'value'] =
None
60 def wait(self, timeout = None):
61 self.event.wait(timeout)
63 if timeout
is not None:
74 return self.event.isSet()
77 return self.event.set()
80 return self.event.clear()
82 def _wrapper(f, result, *args, **kw):
84 result.value = f(*args, **kw)
87 def threaded(f, *args, **kw):
91 start_new_thread(_wrapper, (f, result) + args, kw)
95 class RemoteObjectHandler:
97 def __init__(self, kill_on_error = 0, signal_file = '', debug = False):
101 self.kill_on_error = kill_on_error
102 self.signal_file = signal_file
104 self.bindings = {MSG_QUIT: self.quit,
105 MSG_GET_ATTR: self.getattr,
106 MSG_SET_ATTR: self.setattr,
107 MSG_CALL_METHOD: self.call_method,
108 MSG_TERMINATE: self.terminate,
109 MSG_GET_TIME: self.get_time}
114 self.t_expire = 30. * 60
119 def send(self, msg, value = None):
120 raise NotImplementedError
123 raise NotImplementedError
125 def setattr(self, name, val):
127 setattr(self.object, name, val)
128 self.send(MSG_SETATTR,
None)
130 def getattr(self, name):
132 val = getattr(self.object, name)
133 self.send(MSG_GET_ATTR, val)
135 def call_method(self, key, name, args, kw):
139 method = getattr(self.object, name)
143 result = method(*args, **kw)
147 self.append_time(t, t - t0)
151 self.send(MSG_CALL_METHOD_RESULT, (key, result, t))
153 def append_time(self, time_stamp, delta_t):
155 delta_t = abs(delta_t)
157 if delta_t > 100 * 24. * 60. * 60.:
160 self.times.append({
'time_stamp': time_stamp,
'delta': delta_t})
162 self.times = [d
for d
in self.times
if \
163 abs(time_stamp - d[
'time_stamp']) < self.t_expire]
167 times = [t[
'delta']
for t
in self.times]
170 t = sum(times) / len(times)
176 def set_object(self, o):
179 def initialize(self):
180 raise NotImplementedError
183 raise NotImplementedError
186 raise NotImplementedError
196 class RemoteObject(object):
198 def __init__(self, op, handler_tid, manager):
200 object.__init__(self)
203 attr_name =
'_%s__handler_tid' % self.__class__.__name__
205 object.__setattr__(self, attr_name, handler_tid)
206 self.__dict__[
'__handler_tid'] = handler_tid
208 attr_name =
'_%s__manager' % self.__class__.__name__
209 object.__setattr__(self, attr_name, manager)
210 self.__dict__[
'__manager'] = manager
212 self.__dict__[
'_op'] = op
217 return self.__dict__[
'_%s__handler_tid' % self.__class__.__name__]
219 def _call_method(self, name, *args, **kw):
220 raise NotImplementedError
222 def _get_attr(self, value):
225 self.__manager.send(self.__handler_tid, MSG_GET_TIME, value)
227 return self.__manager.recv(self.__handler_tid, MSG_GET_ATTR)
231 self.__manager.send(self.__handler_tid, MSG_GET_TIME,
None)
233 return self.__manager.recv(self.__handler_tid, MSG_GET_TIME)
235 def __getattr__(self, name):
240 if hasattr(self._op, name):
242 val = getattr(self._op, name)
245 return lambda *args, **kw: self._call_method(name, *args, **kw)
248 return self._get_attr(name)
251 raise AttributeError,
'Attribute %s does not exist' % name
253 def __setattr__(self, name, val):
258 if hasattr(self, name):
259 object.__setattr__(self, name, val)
261 self.__manager.send(self.__handler_tid, MSG_SET_ATTR, (name, val))
263 if __name__ ==
'__main__':
264 ro = RemoteObjectHandler()