class ThreadedExecutor(object): def __init__(self, name, queue=Queue()): self.name = name self.comms = Comms(self) self.km = None self.kc = None self.execution_count = 0 Channel.queue = queue self.start() def start(self): self.km = KernelManager(kernel_name='python', client_class='execute.Client') self.km.start_kernel() self.kc = self.km.client() self.kc.start_channels() time.sleep(2) atexit.register(self.shutdown_kernel) def restart_kernel(self): self.execution_count = 0 self.km.restart_kernel() Channel.executions = OrderedDict() def interrupt_kernel(self): self.km.interrupt_kernel() def shutdown_kernel(self): # TODO: Shutdown kernel but keep labmode running self.execution_count = 0 self.km.request_shutdown() self.km.cleanup() self.km.finish_shutdown() logging.info('Shutting down') def kernel_info(self): self.kc.kernel_info() def __call__(self, code, stop_on_error=True, cell=None, silent=False): "If stop_on_error is True, execution may stop on exceptions" if not silent: self.execution_count += 1 if cell: cell.prompt = self.execution_count self.kc.execute(code, silent=silent, store_history=True, # Has to be true to make execution counter work allow_stdin=False, stop_on_error=stop_on_error)
class ThreadedExecutor(object): def __init__(self, name, queue=Queue()): self.name = name self.comms = Comms(self) self.km = None self.kc = None self.execution_count = 0 self.executable = None Channel.queue = queue def start(self, cwd=None, executable=None): self.km = KernelManager(kernel_name='python', client_class='nei.execute.Client') self.executable = executable if executable is not None: self.km.kernel_spec.argv[0] = executable self.km.start_kernel(**({} if cwd is None else {'cwd': cwd})) self.kc = self.km.client() self.kc.start_channels() time.sleep(2) atexit.register(self.shutdown_kernel) def reset(self): Channel.executions = 0 def restart_kernel(self): self.execution_count = 0 self.km.restart_kernel() Channel.executions = 0 def interrupt_kernel(self): self.km.interrupt_kernel() def shutdown_kernel(self): # TODO: Shutdown kernel but keep nei running self.execution_count = 0 self.km.request_shutdown() self.km.cleanup() self.km.finish_shutdown() logging.info('Shutting down') def kernel_info(self): self.kc.kernel_info() def complete(self, code, position): self.kc.complete(code, position) def __call__(self, code, stop_on_error=True, cell=None, silent=False): "If stop_on_error is True, execution may stop on exceptions" if not silent: self.execution_count += 1 if cell: cell.prompt = self.execution_count self.kc.execute( code, silent=silent, store_history=True, # Has to be true to make execution counter work allow_stdin=False, stop_on_error=stop_on_error)