Bläddra i källkod

Shutdown taipy gracefully without long traceback (#1595)

* Handle Keyboard Interrupt Error
* Handle KeyboardInterrupt with gevent
Pratham Gupta 9 månader sedan
förälder
incheckning
bd686a442e
2 ändrade filer med 14 tillägg och 7 borttagningar
  1. 8 5
      taipy/_cli/_run_cli.py
  2. 6 2
      taipy/gui/server.py

+ 8 - 5
taipy/_cli/_run_cli.py

@@ -54,10 +54,13 @@ class _RunCLI(_AbstractCLI):
 
         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)

+ 6 - 2
taipy/gui/server.py

@@ -256,8 +256,9 @@ class _Server:
 
     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"):
@@ -318,7 +319,10 @@ class _Server:
         # 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: