| 1 |
Pending Kernels |
| 2 |
=============== |
| 3 |
|
| 4 |
*Added in 7.1.0* |
| 5 |
|
| 6 |
In scenarios where an kernel takes a long time to start (e.g. kernels running |
| 7 |
remotely), it can be advantageous to immediately return the kernel's model and |
| 8 |
ID from key methods like ``.start_kernel()`` and ``.shutdown_kernel()``. The |
| 9 |
kernel will continue its task without blocking other managerial actions. |
| 10 |
|
| 11 |
This intermediate state is called a **"pending kernel"**. |
| 12 |
|
| 13 |
How they work |
| 14 |
------------- |
| 15 |
|
| 16 |
When ``.start_kernel()`` or ``.shutdown_kernel()`` is called, a ``Future`` is |
| 17 |
created under the ``KernelManager.ready`` property. This property can be |
| 18 |
awaited anytime to ensure that the kernel moves out of its pending state, e.g.: |
| 19 |
|
| 20 |
.. code-block:: python |
| 21 |
|
| 22 |
# await a Kernel Manager's `.ready` property to |
| 23 |
# block further action until the kernel is out |
| 24 |
# of its pending state. |
| 25 |
await kernel_manager.ready |
| 26 |
|
| 27 |
Once the kernel is finished pending, ``.ready.done()`` will be ``True`` and |
| 28 |
either 1) ``.ready.result()`` will return ``None`` or 2) ``.ready.exception()`` |
| 29 |
will return a raised exception |
| 30 |
|
| 31 |
Using pending kernels |
| 32 |
--------------------- |
| 33 |
|
| 34 |
The most common way to interact with pending kernels is through the `` |
| 35 |
MultiKernelManager``—the object that manages a collection of kernels—by setting |
| 36 |
its ``use_pending_kernels`` trait to ``True``. Pending kernels are "opt-in"; |
| 37 |
they are not used by default in the ``MultiKernelManager``. |
| 38 |
|
| 39 |
When ``use_pending_kernels`` is ``True``, the following changes are made to the |
| 40 |
``MultiKernelManager``: |
| 41 |
|
| 42 |
1. ``start_kernel`` and ``stop_kernel`` return immediately while running the |
| 43 |
pending task in a background thread. |
| 44 |
2. The following methods raise a ``RuntimeError`` if a kernel is pending: |
| 45 |
* ``restart_kernel`` |
| 46 |
* ``interrupt_kernel`` |
| 47 |
* ``shutdown_kernel`` |
| 48 |
3. ``shutdown_all`` will wait for all pending kernels to become ready before |
| 49 |
attempting to shut them down. |