-
Notifications
You must be signed in to change notification settings - Fork 390
Description
Bug description
I have developed a custom kernel for Matlab based on ipykernel. The kernel works well for my purposes, appears to be used by a few others, I haven't changed it in months, and I use it on a regular basis for Quarto documents.
Recently I noticed that processing of Quarto documents using the kernel is very slow, even for very simple computations, and then figured out that this is due to the kernel no longer being daemonized. Every time the document is processed, the kernel and thereby Matlab is started new.
Steps to reproduce
This assumes that Matlab is installed.
-
install the kernel from https://github.com/allefeld/mkernel
-
if installed in an environment (e.g. conda), activate the environment
-
create a file
test.qmdwith the contents
---
jupyter: mkernel
---
```{matlab}
2 + 2
```-
run
quarto preview test.qmd -
edit and save
test.qmd
Expected behavior
After the first start of quarto preview, the kernel should be started, but on subsequent renders triggered by editing and saving the file, Quarto should re-use the kernel.
Actual behavior
The kernel is shut down after each render and restarted on the next.
$ quarto preview test.qmd
Starting mkernel kernel...Done
Executing 'test.quarto_ipynb'
Cell 1/1: ''...Done
– snip –
Output created: test.html
Watching files for changes
Browse at http://localhost:6681/
Opening in existing browser session.
Starting mkernel kernel...Done
Executing 'test.quarto_ipynb'
Cell 1/1: ''...Done
– snip –
Output created: test.html
Watching files for changes
I checked the logs of my kernel, and indeed it receives a shutdown request, e.g.
Control received: {'header': {'msg_id': '6297427c-daf6f0379e7b84702c45a4c4_36565_0', 'msg_type': 'shutdown_request', 'username': 'ca', 'session': '6297427c-daf6f0379e7b84702c45a4c4', 'date': datetime.datetime(2024, 3, 15, 21, 40, 2, 470311, tzinfo=tzutc()), 'version': '5.3'}, 'msg_id': '6297427c-daf6f0379e7b84702c45a4c4_36565_0', 'msg_type': 'shutdown_request', 'parent_header': {}, 'metadata': {}, 'content': {'restart': False}, 'buffers': []}
To check whether this is a general problem with Jupyter kernels, in test.qmd I replaced mkernel with python3 and matlab with python, and the Python kernel is daemonized as expected.
To see what the difference between handling of the python3 kernel and my mkernel kernel is, I used the option --execute-debug.
Full output of quarto preview test.qmd --execute-debug, including modifying test.qmd once.
- Connecting to kernel
Starting mkernel kernel...2024-03-15 22:25:05,381 - root - Level 25 - starting notebook server subprocess
2024-03-15 22:25:05,381 - root - Level 25 - starting notebook server subprocess
2024-03-15 22:25:05,642 - root - Level 25 - running notebook server subprocess
2024-03-15 22:25:05,642 - root - Level 25 - creating notebook server (tcp: /run/user/1000/jt/3c0b0e66a9a6f7a52edd)
2024-03-15 22:25:05,643 - root - Level 25 - notebook server bound to port 36549
- Kernel connection successful
2024-03-15 22:25:05,723 - root - Level 25 - handling server request
- Sending execute command to kernel
- Execute command sent, reading response
2024-03-15 22:25:05,725 - root - Level 25 - executing notebook
2024-03-15 22:25:05,725 - root - Level 25 - inside notebook_execute
2024-03-15 22:25:05,760 - root - Level 25 - read notebook
2024-03-15 22:25:05,761 - root - Level 25 - not using cache
2024-03-15 22:25:05,761 - root - Level 25 - Will attempt to create notebook
2024-03-15 22:25:05,761 - root - Level 25 - type of notebook: <class 'nbformat.notebooknode.NotebookNode'>
2024-03-15 22:25:05,761 - root - Level 25 - Creating NotebookClient
2024-03-15 22:25:14,554 - root - Level 25 - NotebookClient created
2024-03-15 22:25:14,555 - root - Level 25 - Executing cell 0
2024-03-15 22:25:14,555 - root - Level 25 - Executed cell 0
2024-03-15 22:25:14,555 - root - Level 25 - Executing cell 1
Done
Executing 'test.quarto_ipynb'
Cell 1/1: ''...2024-03-15 22:25:21,150 - root - Level 25 - Executed cell 1
2024-03-15 22:25:21,150 - root - Level 25 - Done
2024-03-15 22:25:21,150 - root - Level 25 - Notebook execution complete
2024-03-15 22:25:21,152 - root - Level 25 - notebook not persistable (exiting server)
2024-03-15 22:25:21,152 - root - Level 25 - cleaning up server resources
2024-03-15 22:25:21,152 - root - Level 25 - exiting server
Done
- Server request complete
pandoc
to: html
output-file: test.html
standalone: true
section-divs: true
html-math-method: mathjax
wrap: none
default-image-extension: png
metadata
document-css: false
link-citations: true
date-format: long
lang: en
jupyter: mkernel
Output created: test.html
Watching files for changes
Browse at http://localhost:6310/
Opening in existing browser session.
- Connecting to kernel
Starting mkernel kernel...2024-03-15 22:25:48,426 - root - Level 25 - starting notebook server subprocess
2024-03-15 22:25:48,426 - root - Level 25 - starting notebook server subprocess
2024-03-15 22:25:48,722 - root - Level 25 - running notebook server subprocess
2024-03-15 22:25:48,722 - root - Level 25 - creating notebook server (tcp: /run/user/1000/jt/3c0b0e66a9a6f7a52edd)
2024-03-15 22:25:48,722 - root - Level 25 - notebook server bound to port 46199
- Kernel connection successful
- Sending execute command to kernel
2024-03-15 22:25:48,771 - root - Level 25 - handling server request
- Execute command sent, reading response
2024-03-15 22:25:48,772 - root - Level 25 - executing notebook
2024-03-15 22:25:48,772 - root - Level 25 - inside notebook_execute
2024-03-15 22:25:48,815 - root - Level 25 - read notebook
2024-03-15 22:25:48,816 - root - Level 25 - not using cache
2024-03-15 22:25:48,816 - root - Level 25 - Will attempt to create notebook
2024-03-15 22:25:48,816 - root - Level 25 - type of notebook: <class 'nbformat.notebooknode.NotebookNode'>
2024-03-15 22:25:48,816 - root - Level 25 - Creating NotebookClient
2024-03-15 22:25:57,862 - root - Level 25 - NotebookClient created
Done
2024-03-15 22:25:57,863 - root - Level 25 - Executing cell 0
2024-03-15 22:25:57,864 - root - Level 25 - Executed cell 0
2024-03-15 22:25:57,864 - root - Level 25 - Executing cell 1
Executing 'test.quarto_ipynb'
Cell 1/1: ''...2024-03-15 22:26:04,547 - root - Level 25 - Executed cell 1
2024-03-15 22:26:04,547 - root - Level 25 - Done
2024-03-15 22:26:04,547 - root - Level 25 - Notebook execution complete
Done
2024-03-15 22:26:04,548 - root - Level 25 - notebook not persistable (exiting server)
2024-03-15 22:26:04,548 - root - Level 25 - cleaning up server resources
- Server request complete
pandoc
to: html
output-file: test.html
standalone: true
section-divs: true
html-math-method: mathjax
wrap: none
default-image-extension: png
metadata
document-css: false
link-citations: true
date-format: long
lang: en
jupyter: mkernel
Output created: test.html
Watching files for changes
The difference is that with mkernel compared to python3, there are additional lines between Notebook execution complete and - Server request complete:
2024-03-15 22:25:21,152 - root - Level 25 - notebook not persistable (exiting server)
2024-03-15 22:25:21,152 - root - Level 25 - cleaning up server resources
2024-03-15 22:25:21,152 - root - Level 25 - exiting server
Done
The important bit seems to be notebook not persistable (exiting server), but unfortunately a web search turned up nothing.
Your environment
- Debian 12.5
Quarto check output
Quarto 1.5.25
[✓] Checking versions of quarto binary dependencies...
Pandoc version 3.1.11: OK
Dart Sass version 1.70.0: OK
Deno version 1.41.0: OK
[✓] Checking versions of quarto dependencies......OK
[✓] Checking Quarto installation......OK
Version: 1.5.25
Path: /opt/quarto/bin
[✓] Checking tools....................OK
TinyTeX: (not installed)
Chromium: (not installed)
[✓] Checking LaTeX....................OK
Using: Installation From Path
Path: /usr/bin
Version: 2022
[✓] Checking basic markdown render....OK
[✓] Checking Python 3 installation....OK
Version: 3.10.12 (Conda)
Path: /home/ca/Store/micromamba/envs/std/bin/python
Jupyter: 5.3.1
Kernels: python3, mkernel
[✓] Checking Jupyter engine render....OK
[✓] Checking R installation...........OK
Version: 4.3.3
Path: /usr/lib/R
LibPaths:
- /home/ca/Store/R/4.3
- /usr/local/lib/R/site-library
- /usr/lib/R/site-library
- /usr/lib/R/library
knitr: 1.45
rmarkdown: 2.25
[✓] Checking Knitr engine render......OK