瀏覽代碼

Improve handling of KeyboardInterrupt in CLI applications (#567)

Max Ryabinin 2 年之前
父節點
當前提交
7a91d03ab2
共有 2 個文件被更改,包括 8 次插入5 次删除
  1. 8 3
      hivemind/hivemind_cli/run_dht.py
  2. 0 2
      hivemind/hivemind_cli/run_server.py

+ 8 - 3
hivemind/hivemind_cli/run_dht.py

@@ -84,9 +84,14 @@ def main():
     )
     log_visible_maddrs(dht.get_visible_maddrs(), only_p2p=args.use_ipfs)
 
-    while True:
-        dht.run_coroutine(report_status, return_future=False)
-        time.sleep(args.refresh_period)
+    try:
+        while True:
+            dht.run_coroutine(report_status, return_future=False)
+            time.sleep(args.refresh_period)
+    except KeyboardInterrupt:
+        logger.info("Caught KeyboardInterrupt, shutting down")
+    finally:
+        dht.shutdown()
 
 
 if __name__ == "__main__":

+ 0 - 2
hivemind/hivemind_cli/run_server.py

@@ -108,8 +108,6 @@ def main():
         server.join()
     except KeyboardInterrupt:
         logger.info("Caught KeyboardInterrupt, shutting down")
-    finally:
-        server.shutdown()
 
 
 if __name__ == "__main__":