Skip to content

Commit 08f0e81

Browse files
committed
#786: updates to shutdown
1 parent 57f3721 commit 08f0e81

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

dtale/app.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@
5050
)
5151
from dtale.views import DtaleData, head_endpoint, is_up, kill, startup
5252

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

6055
USE_NGROK = False
@@ -423,8 +418,12 @@ def shutdown_server():
423418
logger.info("Executing shutdown...")
424419
func = request.environ.get("werkzeug.server.shutdown")
425420
if func is None:
426-
raise RuntimeError("Not running with the Werkzeug Server")
427-
func()
421+
logger.info("Not running with the Werkzeug Server, exiting using os._exit")
422+
import os
423+
424+
os._exit(os.EX_OK)
425+
else:
426+
func()
428427
global_state.cleanup()
429428
ACTIVE_PORT = None
430429
ACTIVE_HOST = None
@@ -830,7 +829,10 @@ def _start():
830829
if is_active:
831830
_start()
832831
else:
833-
_thread.start_new_thread(_start, ())
832+
import multiprocessing
833+
834+
p = multiprocessing.Process(target=_start, args=())
835+
p.start()
834836

835837
if final_options["notebook"]:
836838
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):

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

+9
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,16 @@ 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:
441+
if parse_version(werkzeug.__version__) >= parse_version("2.1.0"):
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()
447+
return
439448
try:
440449
c.get("/shutdown")
441450
unittest.fail()

0 commit comments

Comments
 (0)