Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run executenb on a ThreadPoolExecutor #364

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion voila/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
#############################################################################

import os
from concurrent.futures import ThreadPoolExecutor

import tornado.web
from tornado.concurrent import run_on_executor

from jupyter_server.base.handlers import JupyterHandler
from jupyter_server.config_manager import recursive_update
Expand All @@ -21,6 +23,8 @@

class VoilaHandler(JupyterHandler):

executor = ThreadPoolExecutor()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From 3.5+, if not specified, max_workers defaults to "to the number of processors on the machine, multiplied by 5".

https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor


def initialize(self, **kwargs):
self.notebook_path = kwargs.pop('notebook_path', []) # should it be []
self.nbconvert_template_paths = kwargs.pop('nbconvert_template_paths', [])
Expand Down Expand Up @@ -58,7 +62,7 @@ def get(self, path=None):
cwd = os.path.dirname(notebook_path)
kernel_id = yield tornado.gen.maybe_future(self.kernel_manager.start_kernel(kernel_name=notebook.metadata.kernelspec.name, path=cwd))
km = self.kernel_manager.get_kernel(kernel_id)
result = executenb(notebook, km=km, cwd=cwd, config=self.traitlet_config)
result = yield self.execute_notebook(notebook, km=km, cwd=cwd, config=self.traitlet_config)

# render notebook to html
resources = {
Expand Down Expand Up @@ -102,6 +106,11 @@ def filter_empty_code_cells(cell):
def redirect_to_file(self, path):
self.redirect(url_path_join(self.base_url, 'voila', 'files', path))

@run_on_executor
def execute_notebook(self, notebook, km, cwd, config):
"""Execute a notebook asynchronously on the executor"""
return executenb(notebook, km=km, cwd=cwd, config=config)

@tornado.gen.coroutine
def load_notebook(self, path):
model = self.contents_manager.get(path=path)
Expand Down