Skip to content

Commit 0a27af8

Browse files
committed
#786: updates to shutdown
1 parent 57f3721 commit 0a27af8

File tree

6 files changed

+41
-19
lines changed

6 files changed

+41
-19
lines changed

dtale/app.py

+18-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import getpass
44
import jinja2
55
import logging
6+
import multiprocessing
67
import os
78
import pandas as pd
89
import random
@@ -50,11 +51,6 @@
5051
)
5152
from dtale.views import DtaleData, head_endpoint, is_up, kill, startup
5253

53-
if PY3:
54-
import _thread
55-
else:
56-
import thread as _thread
57-
5854
logger = logging.getLogger(__name__)
5955

6056
USE_NGROK = False
@@ -423,8 +419,12 @@ def shutdown_server():
423419
logger.info("Executing shutdown...")
424420
func = request.environ.get("werkzeug.server.shutdown")
425421
if func is None:
426-
raise RuntimeError("Not running with the Werkzeug Server")
427-
func()
422+
logger.info("Not running with the Werkzeug Server, exiting using os._exit")
423+
import os
424+
425+
os._exit(os.EX_OK)
426+
else:
427+
func()
428428
global_state.cleanup()
429429
ACTIVE_PORT = None
430430
ACTIVE_HOST = None
@@ -633,6 +633,14 @@ def use_colab(port):
633633
return None
634634

635635

636+
class _LocalFunctions:
637+
@classmethod
638+
def add_functions(cls, *args):
639+
for function in args:
640+
setattr(cls, function.__name__, function)
641+
function.__qualname__ = cls.__qualname__ + "." + function.__name__
642+
643+
636644
def show(data=None, data_loader=None, name=None, context_vars=None, **options):
637645
"""
638646
Entry point for kicking off D-Tale :class:`flask:flask.Flask` process from python process
@@ -830,7 +838,9 @@ def _start():
830838
if is_active:
831839
_start()
832840
else:
833-
_thread.start_new_thread(_start, ())
841+
_LocalFunctions.add_functions(_start)
842+
p = multiprocessing.Process(target=_start, args=())
843+
p.start()
834844

835845
if final_options["notebook"]:
836846
instance.notebook()

dtale/views.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,10 @@ def kill(base):
192192
This function fires a request to this instance's 'shutdown' route to kill it
193193
194194
"""
195-
requests.get(build_shutdown_url(base))
195+
try:
196+
requests.get(build_shutdown_url(base))
197+
except BaseException:
198+
logger.info("Shutdown complete")
196199

197200

198201
def is_up(base):

requirements.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Flask<2.3; python_version >= '3.7'
2323
Flask-Compress
2424
flask-ngrok; python_version > '3.0'
2525
future>=0.14.0
26+
immutables<=0.19; python_version == '3.6'
2627
itsdangerous<=1.1.0; python_version < '3.7'
2728
itsdangerous; python_version >= '3.7'
2829
# required for loading scikit-learn
@@ -45,7 +46,8 @@ matplotlib<=3.3.4; python_version == '3.4'
4546
matplotlib<=3.3.4; python_version == '3.5'
4647
matplotlib<=3.3.4; python_version == '3.6'
4748
matplotlib<=3.5.3; python_version == '3.7'
48-
matplotlib; python_version >= '3.8'
49+
matplotlib<=3.7.2; python_version == '3.8'
50+
matplotlib; python_version >= '3.9'
4951
missingno
5052
networkx<=2.2; python_version <= '3.4'
5153
networkx<=2.4; python_version == '3.5'

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def run_tests(self):
8383
install_requires=read_file("requirements.txt"),
8484
extras_require={
8585
"arctic": ["arctic <= 1.79.4"],
86-
"arcticdb": ["arcticdb"],
86+
"arcticdb": ["arcticdb != 1.6.1"],
8787
"dash-bio": [
8888
"ParmEd==3.4.3; python_version == '3.6'",
8989
"dash-bio; python_version > '3.0'",

tests/dtale/test_app.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ def run(self, *args, **kwargs):
320320
ignore_duplicate=True,
321321
)
322322

323-
with mock.patch("dtale.app._thread.start_new_thread", mock.Mock()) as mock_thread:
323+
with mock.patch("multiprocessing.Process", mock.Mock()) as mock_thread:
324324
show(data=test_data, subprocess=True, ignore_duplicate=True)
325325
mock_thread.assert_called()
326326

tests/dtale/test_views.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -435,18 +435,25 @@ def import_mock(name, *args, **kwargs):
435435

436436
@pytest.mark.unit
437437
def test_shutdown(unittest):
438+
import werkzeug
439+
438440
with app.test_client() as c:
439441
try:
440-
c.get("/shutdown")
442+
with ExitStack() as stack:
443+
mock_os = stack.enter_context(mock.patch("os._exit", mock.Mock()))
444+
resp = c.get("/shutdown").data
445+
assert "Server shutting down..." in str(resp)
446+
mock_os.assert_called()
441447
unittest.fail()
442448
except: # noqa
443449
pass
444-
mock_shutdown = mock.Mock()
445-
resp = c.get(
446-
"/shutdown", environ_base={"werkzeug.server.shutdown": mock_shutdown}
447-
).data
448-
assert "Server shutting down..." in str(resp)
449-
mock_shutdown.assert_called()
450+
if parse_version(werkzeug.__version__) < parse_version("2.1.0"):
451+
mock_shutdown = mock.Mock()
452+
resp = c.get(
453+
"/shutdown", environ_base={"werkzeug.server.shutdown": mock_shutdown}
454+
).data
455+
assert "Server shutting down..." in str(resp)
456+
mock_shutdown.assert_called()
450457

451458

452459
@pytest.mark.unit

0 commit comments

Comments
 (0)