From e0171ae7239741a9f38e24d41f77cd68c2f9d285 Mon Sep 17 00:00:00 2001 From: David Brochart Date: Tue, 11 Jan 2022 09:28:06 +0100 Subject: [PATCH] Don't block the event loop when exporting with nbconvert --- jupyter_server/nbconvert/handlers.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/jupyter_server/nbconvert/handlers.py b/jupyter_server/nbconvert/handlers.py index 7968e85514..29a8f10e51 100644 --- a/jupyter_server/nbconvert/handlers.py +++ b/jupyter_server/nbconvert/handlers.py @@ -5,6 +5,7 @@ import os import zipfile +from anyio.to_thread import run_sync from ipython_genutils import text from ipython_genutils.py3compat import cast_bytes from nbformat import from_dict @@ -115,8 +116,11 @@ async def get(self, format, path): if ext_resources_dir: resource_dict["metadata"]["path"] = ext_resources_dir + # Exporting can take a while, delegate to a thread so we don't block the event loop try: - output, resources = exporter.from_notebook_node(nb, resources=resource_dict) + output, resources = await run_sync( + exporter.from_notebook_node(nb, resources=resource_dict) + ) except Exception as e: self.log.exception("nbconvert failed: %s", e) raise web.HTTPError(500, "nbconvert failed: %s" % e) from e