class FCSynchronousMultiEngineFromMultiEngine(Referenceable): """Adapt `IMultiEngine` -> `ISynchronousMultiEngine` -> `IFCSynchronousMultiEngine`. """ implements(IFCSynchronousMultiEngine, IFCClientInterfaceProvider) addSlash = True def __init__(self, multiengine): # Adapt the raw multiengine to `ISynchronousMultiEngine` before saving # it. This allow this class to do two adaptation steps. self.smultiengine = ISynchronousMultiEngine(multiengine) self._deferredIDCallbacks = {} #--------------------------------------------------------------------------- # Non interface methods #--------------------------------------------------------------------------- def packageFailure(self, f): f.cleanFailure() return self.packageSuccess(f) def packageSuccess(self, obj): serial = pickle.dumps(obj, 2) return serial #--------------------------------------------------------------------------- # Things related to PendingDeferredManager #--------------------------------------------------------------------------- @packageResult def remote_get_pending_deferred(self, deferredID, block): d = self.smultiengine.get_pending_deferred(deferredID, block) try: callback = self._deferredIDCallbacks.pop(deferredID) except KeyError: callback = None if callback is not None: d.addCallback(callback[0], *callback[1], **callback[2]) return d @packageResult def remote_clear_pending_deferreds(self): return defer.maybeDeferred(self.smultiengine.clear_pending_deferreds) def _addDeferredIDCallback(self, did, callback, *args, **kwargs): self._deferredIDCallbacks[did] = (callback, args, kwargs) return did #--------------------------------------------------------------------------- # IEngineMultiplexer related methods #--------------------------------------------------------------------------- @packageResult def remote_execute(self, lines, targets, block): return self.smultiengine.execute(lines, targets=targets, block=block) @packageResult def remote_push(self, binaryNS, targets, block): try: namespace = pickle.loads(binaryNS) except: d = defer.fail(failure.Failure()) else: d = self.smultiengine.push(namespace, targets=targets, block=block) return d @packageResult def remote_pull(self, keys, targets, block): d = self.smultiengine.pull(keys, targets=targets, block=block) return d @packageResult def remote_push_function(self, binaryNS, targets, block): try: namespace = pickle.loads(binaryNS) except: d = defer.fail(failure.Failure()) else: namespace = uncanDict(namespace) d = self.smultiengine.push_function(namespace, targets=targets, block=block) return d def _canMultipleKeys(self, result): return [canSequence(r) for r in result] @packageResult def remote_pull_function(self, keys, targets, block): def can_functions(r, keys): if len(keys) == 1 or isinstance(keys, str): result = canSequence(r) elif len(keys) > 1: result = [canSequence(s) for s in r] return result d = self.smultiengine.pull_function(keys, targets=targets, block=block) if block: d.addCallback(can_functions, keys) else: d.addCallback(lambda did: self._addDeferredIDCallback( did, can_functions, keys)) return d @packageResult def remote_push_serialized(self, binaryNS, targets, block): try: namespace = pickle.loads(binaryNS) except: d = defer.fail(failure.Failure()) else: d = self.smultiengine.push_serialized(namespace, targets=targets, block=block) return d @packageResult def remote_pull_serialized(self, keys, targets, block): d = self.smultiengine.pull_serialized(keys, targets=targets, block=block) return d @packageResult def remote_get_result(self, i, targets, block): if i == 'None': i = None return self.smultiengine.get_result(i, targets=targets, block=block) @packageResult def remote_reset(self, targets, block): return self.smultiengine.reset(targets=targets, block=block) @packageResult def remote_keys(self, targets, block): return self.smultiengine.keys(targets=targets, block=block) @packageResult def remote_kill(self, controller, targets, block): return self.smultiengine.kill(controller, targets=targets, block=block) @packageResult def remote_clear_queue(self, targets, block): return self.smultiengine.clear_queue(targets=targets, block=block) @packageResult def remote_queue_status(self, targets, block): return self.smultiengine.queue_status(targets=targets, block=block) @packageResult def remote_set_properties(self, binaryNS, targets, block): try: ns = pickle.loads(binaryNS) except: d = defer.fail(failure.Failure()) else: d = self.smultiengine.set_properties(ns, targets=targets, block=block) return d @packageResult def remote_get_properties(self, keys, targets, block): if keys == 'None': keys = None return self.smultiengine.get_properties(keys, targets=targets, block=block) @packageResult def remote_has_properties(self, keys, targets, block): return self.smultiengine.has_properties(keys, targets=targets, block=block) @packageResult def remote_del_properties(self, keys, targets, block): return self.smultiengine.del_properties(keys, targets=targets, block=block) @packageResult def remote_clear_properties(self, targets, block): return self.smultiengine.clear_properties(targets=targets, block=block) #--------------------------------------------------------------------------- # IMultiEngine related methods #--------------------------------------------------------------------------- def remote_get_ids(self): """Get the ids of the registered engines. This method always blocks. """ return self.smultiengine.get_ids() #--------------------------------------------------------------------------- # IFCClientInterfaceProvider related methods #--------------------------------------------------------------------------- def remote_get_client_name(self): return 'IPython.kernel.multienginefc.FCFullSynchronousMultiEngineClient'
class FCSynchronousMultiEngineFromMultiEngine(Referenceable): """Adapt `IMultiEngine` -> `ISynchronousMultiEngine` -> `IFCSynchronousMultiEngine`. """ implements(IFCSynchronousMultiEngine, IFCClientInterfaceProvider) addSlash = True def __init__(self, multiengine): # Adapt the raw multiengine to `ISynchronousMultiEngine` before saving # it. This allow this class to do two adaptation steps. self.smultiengine = ISynchronousMultiEngine(multiengine) self._deferredIDCallbacks = {} #--------------------------------------------------------------------------- # Non interface methods #--------------------------------------------------------------------------- def packageFailure(self, f): f.cleanFailure() return self.packageSuccess(f) def packageSuccess(self, obj): serial = pickle.dumps(obj, 2) return serial #--------------------------------------------------------------------------- # Things related to PendingDeferredManager #--------------------------------------------------------------------------- @packageResult def remote_get_pending_deferred(self, deferredID, block): d = self.smultiengine.get_pending_deferred(deferredID, block) try: callback = self._deferredIDCallbacks.pop(deferredID) except KeyError: callback = None if callback is not None: d.addCallback(callback[0], *callback[1], **callback[2]) return d @packageResult def remote_clear_pending_deferreds(self): return defer.maybeDeferred(self.smultiengine.clear_pending_deferreds) def _addDeferredIDCallback(self, did, callback, *args, **kwargs): self._deferredIDCallbacks[did] = (callback, args, kwargs) return did #--------------------------------------------------------------------------- # IEngineMultiplexer related methods #--------------------------------------------------------------------------- @packageResult def remote_execute(self, lines, targets, block): return self.smultiengine.execute(lines, targets=targets, block=block) @packageResult def remote_push(self, binaryNS, targets, block): try: namespace = pickle.loads(binaryNS) except: d = defer.fail(failure.Failure()) else: d = self.smultiengine.push(namespace, targets=targets, block=block) return d @packageResult def remote_pull(self, keys, targets, block): d = self.smultiengine.pull(keys, targets=targets, block=block) return d @packageResult def remote_push_function(self, binaryNS, targets, block): try: namespace = pickle.loads(binaryNS) except: d = defer.fail(failure.Failure()) else: namespace = uncanDict(namespace) d = self.smultiengine.push_function(namespace, targets=targets, block=block) return d def _canMultipleKeys(self, result): return [canSequence(r) for r in result] @packageResult def remote_pull_function(self, keys, targets, block): def can_functions(r, keys): if len(keys)==1 or isinstance(keys, str): result = canSequence(r) elif len(keys)>1: result = [canSequence(s) for s in r] return result d = self.smultiengine.pull_function(keys, targets=targets, block=block) if block: d.addCallback(can_functions, keys) else: d.addCallback(lambda did: self._addDeferredIDCallback(did, can_functions, keys)) return d @packageResult def remote_push_serialized(self, binaryNS, targets, block): try: namespace = pickle.loads(binaryNS) except: d = defer.fail(failure.Failure()) else: d = self.smultiengine.push_serialized(namespace, targets=targets, block=block) return d @packageResult def remote_pull_serialized(self, keys, targets, block): d = self.smultiengine.pull_serialized(keys, targets=targets, block=block) return d @packageResult def remote_get_result(self, i, targets, block): if i == 'None': i = None return self.smultiengine.get_result(i, targets=targets, block=block) @packageResult def remote_reset(self, targets, block): return self.smultiengine.reset(targets=targets, block=block) @packageResult def remote_keys(self, targets, block): return self.smultiengine.keys(targets=targets, block=block) @packageResult def remote_kill(self, controller, targets, block): return self.smultiengine.kill(controller, targets=targets, block=block) @packageResult def remote_clear_queue(self, targets, block): return self.smultiengine.clear_queue(targets=targets, block=block) @packageResult def remote_queue_status(self, targets, block): return self.smultiengine.queue_status(targets=targets, block=block) @packageResult def remote_set_properties(self, binaryNS, targets, block): try: ns = pickle.loads(binaryNS) except: d = defer.fail(failure.Failure()) else: d = self.smultiengine.set_properties(ns, targets=targets, block=block) return d @packageResult def remote_get_properties(self, keys, targets, block): if keys=='None': keys=None return self.smultiengine.get_properties(keys, targets=targets, block=block) @packageResult def remote_has_properties(self, keys, targets, block): return self.smultiengine.has_properties(keys, targets=targets, block=block) @packageResult def remote_del_properties(self, keys, targets, block): return self.smultiengine.del_properties(keys, targets=targets, block=block) @packageResult def remote_clear_properties(self, targets, block): return self.smultiengine.clear_properties(targets=targets, block=block) #--------------------------------------------------------------------------- # IMultiEngine related methods #--------------------------------------------------------------------------- def remote_get_ids(self): """Get the ids of the registered engines. This method always blocks. """ return self.smultiengine.get_ids() #--------------------------------------------------------------------------- # IFCClientInterfaceProvider related methods #--------------------------------------------------------------------------- def remote_get_client_name(self): return 'IPython.kernel.multienginefc.FCFullSynchronousMultiEngineClient'