Skip to content

Commit

Permalink
Shutdown taipy gracefully without long traceback (#1595)
Browse files Browse the repository at this point in the history
* Handle Keyboard Interrupt Error
* Handle KeyboardInterrupt with gevent
  • Loading branch information
PrathamGupta06 authored Aug 2, 2024
1 parent f156ec4 commit bd686a4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
13 changes: 8 additions & 5 deletions taipy/_cli/_run_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,13 @@ def handle_command(cls):

taipy_args = [f"--taipy-{arg[2:]}" if arg.startswith("--") else arg for arg in all_args]

subprocess.run(
[sys.executable, args.application_main_file, *(external_args + taipy_args)],
stdout=sys.stdout,
stderr=sys.stdout,
)
try:
subprocess.run(
[sys.executable, args.application_main_file, *(external_args + taipy_args)],
stdout=sys.stdout,
stderr=sys.stdout,
)
except KeyboardInterrupt:
pass

sys.exit(0)
8 changes: 6 additions & 2 deletions taipy/gui/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,9 @@ def _get_async_mode(self) -> str:

def _apply_patch(self):
if self._get_async_mode() == "gevent" and util.find_spec("gevent"):
from gevent import monkey
from gevent import get_hub, monkey

get_hub().NOT_ERROR += (KeyboardInterrupt, )
if not monkey.is_module_patched("time"):
monkey.patch_time()
if self._get_async_mode() == "eventlet" and util.find_spec("eventlet"):
Expand Down Expand Up @@ -318,7 +319,10 @@ def run(self, host, port, debug, use_reloader, flask_log, run_in_thread, allow_u
# flask-socketio specific conditions for 'allow_unsafe_werkzeug' parameters to be popped out of kwargs
if self._get_async_mode() == "threading" and (not sys.stdin or not sys.stdin.isatty()):
run_config = {**run_config, "allow_unsafe_werkzeug": allow_unsafe_werkzeug}
self._ws.run(**run_config)
try:
self._ws.run(**run_config)
except KeyboardInterrupt:
pass

def stop_thread(self):
if hasattr(self, "_thread") and self._thread.is_alive() and self._is_running:
Expand Down

0 comments on commit bd686a4

Please sign in to comment.