Skip to content

Commit 3927d28

Browse files
authored
Merge pull request #512 from hMED22/make-nbconvert-root-handler-asynchronous
Make nbconvert root handler asynchronous
2 parents 858b6a7 + 30f8d5b commit 3927d28

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

jupyter_server/services/nbconvert/handlers.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import json
22

3+
try:
4+
from anyio.to_thread import run_sync
5+
except ImportError:
6+
# fallback on anyio v2 for python version < 3.7
7+
from anyio import run_sync_in_worker_thread as run_sync
8+
39
from tornado import web
410

511
from ...base.handlers import APIHandler
@@ -8,16 +14,18 @@
814
class NbconvertRootHandler(APIHandler):
915

1016
@web.authenticated
11-
def get(self):
17+
async def get(self):
1218
try:
1319
from nbconvert.exporters import base
1420
except ImportError as e:
1521
raise web.HTTPError(500, "Could not import nbconvert: %s" % e) from e
1622
res = {}
17-
exporters = base.get_export_names()
23+
# Some exporters use the filesystem when instantiating, delegate that
24+
# to a thread so we don't block the event loop for it.
25+
exporters = await run_sync(base.get_export_names)
1826
for exporter_name in exporters:
1927
try:
20-
exporter_class = base.get_exporter(exporter_name)
28+
exporter_class = await run_sync(base.get_exporter, exporter_name)
2129
except ValueError:
2230
# I think the only way this will happen is if the entrypoint
2331
# is uninstalled while this method is running

0 commit comments

Comments
 (0)