Skip to content

Commit

Permalink
Fix shutdown deprecation, plotly#63
Browse files Browse the repository at this point in the history
  • Loading branch information
T4rk1n committed Mar 10, 2022
1 parent a6b9adf commit bdd2f00
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
22 changes: 22 additions & 0 deletions jupyter_dash/_stoppable_thread.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import ctypes
import threading


class StoppableThread(threading.Thread):
def get_id(self):
if hasattr(self, "_thread_id"):
return self._thread_id
for thread_id, thread in threading._active.items():
if thread is self:
return thread_id

def kill(self):
thread_id = self.get_id()
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(
ctypes.c_long(thread_id), ctypes.py_object(SystemExit)
)
if res == 0:
raise ValueError(f"Invalid thread id: {thread_id}")
if res > 1:
ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(thread_id), None)
raise SystemExit("Stopping thread failure")
32 changes: 9 additions & 23 deletions jupyter_dash/jupyter_app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import dash
import os
import requests
from flask import request
import flask.cli
from threading import Thread
from retrying import retry
import io
import re
Expand All @@ -21,6 +19,7 @@
from werkzeug.debug.tbtools import get_current_traceback

from .comms import _dash_comm, _jupyter_config, _request_jupyter_config
from ._stoppable_thread import StoppableThread


class JupyterDash(dash.Dash):
Expand All @@ -41,6 +40,8 @@ class JupyterDash(dash.Dash):
_in_colab = "google.colab" in sys.modules
_token = str(uuid.uuid4())

_server_threads = {}

@classmethod
def infer_jupyter_proxy_config(cls):
"""
Expand Down Expand Up @@ -130,15 +131,6 @@ def __init__(self, name=None, server_url=None, **kwargs):

self.server_url = server_url

# Register route to shut down server
@self.server.route('/_shutdown_' + JupyterDash._token, methods=['GET'])
def shutdown():
func = request.environ.get('werkzeug.server.shutdown')
if func is None:
raise RuntimeError('Not running with the Werkzeug Server')
func()
return 'Server shutting down...'

# Register route that we can use to poll to see when server is running
@self.server.route('/_alive_' + JupyterDash._token, methods=['GET'])
def alive():
Expand Down Expand Up @@ -217,7 +209,9 @@ def run_server(
inline_exceptions = mode == "inline"

# Terminate any existing server using this port
self._terminate_server_for_port(host, port)
old_server = self._server_threads.get((host, port))
if old_server:
old_server.kill()

# Configure pathname prefix
requests_pathname_prefix = self.config.get('requests_pathname_prefix', None)
Expand Down Expand Up @@ -291,10 +285,12 @@ def run_server(
def run():
super_run_server(**kwargs)

thread = Thread(target=run)
thread = StoppableThread(target=run)
thread.setDaemon(True)
thread.start()

self._server_threads[(host, port)] = thread

# Wait for server to start up
alive_url = "http://{host}:{port}/_alive_{token}".format(
host=host, port=port, token=JupyterDash._token
Expand Down Expand Up @@ -412,16 +408,6 @@ def _wrap_errors(_):

return html_str, 500

@classmethod
def _terminate_server_for_port(cls, host, port):
shutdown_url = "http://{host}:{port}/_shutdown_{token}".format(
host=host, port=port, token=JupyterDash._token
)
try:
response = requests.get(shutdown_url)
except Exception as e:
pass


def _custom_formatargvalues(
args, varargs, varkw, locals,
Expand Down

0 comments on commit bdd2f00

Please sign in to comment.