async def launch(config, session, context, connection_file): args = [] if config: # configuration passed to the default kernel passes options to ipykernel args = list(filter(lambda x: x != "", config.split("\n"))) km = AsyncKernelManager( kernel_name="python3", # Pass our IPython session as the session for the KernelManager session=session, # Use the same ZeroMQ context that allows for awaiting on recv context=context, connection_file=connection_file, ) # Tack on additional arguments to the underlying kernel await km.start_kernel(extra_arguments=args) return km
def _nbclient(): try: import nbformat from jupyter_client import AsyncKernelManager from nbclient import NotebookClient from ipywidgets import Button # noqa import ipyvtklink # noqa except Exception as exc: return pytest.skip(f'Skipping Notebook test: {exc}') km = AsyncKernelManager(config=None) nb = nbformat.reads(""" { "cells": [ { "cell_type": "code", "execution_count": null, "metadata":{}, "outputs": [], "source":[] } ], "metadata": { "language_info": { "codemirror_mode": { "name": "ipython", "version":3}, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.5" } }, "nbformat": 4, "nbformat_minor": 4 }""", as_version=4) client = NotebookClient(nb, km=km) yield client client._cleanup_kernel()
def _get_ipc_km(self): c = Config() c.KernelManager.transport = 'ipc' c.KernelManager.ip = 'test' km = AsyncKernelManager(config=c) return km
def _get_tcp_km(self): c = Config() km = AsyncKernelManager(config=c) return km
async def start_kernel(self, config=None): # Create a connection file that is named as a child of this kernel base, ext = os.path.splitext(self.parent.connection_file) cf = "{base}-child{ext}".format(base=base, ext=ext,) # TODO: pass config into kernel launch km = AsyncKernelManager( kernel_name="python3", # Pass our IPython session as the session for the KernelManager session=self.session, # Use the same ZeroMQ context that allows for awaiting on recv context=self.future_context, connection_file=cf, # TODO: Figure out if this can be relied on # extra_arguments=["-c", "x = 89898977"], # extra_env={}, ) # Due to how kernel_cmd is no longer in vogue, we have to set # this extra field that just plain has to be set km.extra_env = {} if config is None: config = "" km.kernel_cmd = [ "python3", "-m", "ipykernel_launcher", "-f", "{connection_file}", "-c", f"""the_config = '''{config}''';""", ] self.log.info("launching child kernel with") self.log.info(km.kernel_cmd) await km.start_kernel() kernel = KernelProxy(manager=km, shell_upstream=self.shell_stream) self.iosub.connect(kernel.iopub_url) # Make sure the kernel is really started. We do that by using # kernel_info_requests here. # # In the future we should incorporate kernel logs (output of the kernel process), then # and send back all the information back to the user as display output. # Create a temporary KernelClient for waiting for the kernel to start kc = km.client() kc.start_channels() # Wait for kernel info reply on shell channel while True: self.log.debug("querying kernel info") kc.kernel_info(reply=False) try: msg = await kc.shell_channel.get_msg(timeout=1) except Empty: pass else: if msg["msg_type"] == "kernel_info_reply": # Now we know the kernel is (mostly) ready. # However, most kernels are not quite ready at this point to send # on more execution. # # Do we wait for a further status: idle on iopub? # Wait for idle? # Wait for particular logs from the stdout of the kernel process? break if not await kc.is_alive(): # TODO: Emit child kernel death message into the notebook output raise RuntimeError("Kernel died before replying to kernel_info") self.log.error("Kernel died while launching") # Wait before sending another kernel info request await asyncio.sleep(0.1) # Flush IOPub channel on our (temporary) kernel client while True: try: msg = await kc.iopub_channel.get_msg(timeout=0.2) except Empty: break # Clean up our temporary kernel client kc.stop_channels() # Inform all waiters for the kernel that it is ready self.kernel_launched.set() return kernel
def create_kernel_manager(self): return AsyncKernelManager( kernel_spec_manager=CurrentEnvKernelSpecManager())