class ArmadaMasterProtocol(Protocol): def __init__(self): self.finished = None self.reader = Reader() def queueWork(self, finished, function, *args): self.finished = finished if hasattr(function, '__call__'): if function.__module__ == '__main__': function = '.'.join([os.path.splitext(os.path.basename(sys.modules['__main__'].__file__))[0], function.__name__]) else: function = '.'.join([function.__module__, function.__name__]) self.transport.write(packByte(CONTINUE) + packUTF8(function) + packArray(packUInt, packPickle, args)) return self.finished def kill(self): self.transport.write(packByte(KILL)) def dataReceived(self, data): self.reader.addData(data) try: status = self.reader.readByte() result = self.reader.readPickle() if status == SUCCESS: self.finished.callback(result) elif status == FAILURE: self.finished.errback(result) self.reader.commit() except ReadException: self.reader.revert() @property def working(self): return self.finished is not None
class ArmadaSlaveProtocol(Protocol): def __init__(self): self.reader = Reader() self.killed = Deferred() @inlineCallbacks def dataReceived(self, data): self.reader.addData(data) status = self.reader.readByte() if status == CONTINUE: function = self.reader.readUTF8().split('.') package, function = '.'.join(function[:-1]), function[-1] module = __import__(package, globals(), locals(), ['*'], -1) function = getattr(module, function) args = self.reader.readArray(self.reader.readUInt, self.reader.readPickle) try: result = function(*args) if isinstance(result, Deferred): result = yield result self.transport.write(packByte(SUCCESS) + packPickle(result)) except Exception as ex: self.transport.write(packByte(FAILURE) + packPickle(ex)) elif status == KILL: self.transport.loseConnection() self.killed.callback(True)