From 3e70d8b27b682d64db68e79c8f61712014580530 Mon Sep 17 00:00:00 2001
From: limil <limil.top@qq.com>
Date: Mon, 28 Oct 2024 13:47:25 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E4=B8=80=E4=B8=8Bquit?=
 =?UTF-8?q?=E7=9A=84=E6=83=85=E5=86=B5?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 main.py | 12 +++++++-----
 test.py | 42 ------------------------------------------
 2 files changed, 7 insertions(+), 47 deletions(-)
 delete mode 100644 test.py

diff --git a/main.py b/main.py
index 18f6fff..79e6f1e 100644
--- a/main.py
+++ b/main.py
@@ -81,10 +81,11 @@ class PikpakConsole(cmd2.Cmd):
         return await asyncio.wrap_future(future)
 
     async def aoutput(self, output):
-        async def PrintOuput(output):
-            print(output)
-        future = asyncio.run_coroutine_threadsafe(PrintOuput(output), self.ioLoop)
-        await asyncio.wrap_future(future)
+        return ""
+        # async def PrintOuput(output):
+        #     print(output)
+        # future = asyncio.run_coroutine_threadsafe(PrintOuput(output), self.ioLoop)
+        # await asyncio.wrap_future(future)
 
     async def Run(self):
         # 1. 设置忽略SIGINT
@@ -117,7 +118,8 @@ class PikpakConsole(cmd2.Cmd):
             with self.sigint_protection:
                 if saved_readline_settings is not None:
                     self._restore_readline(saved_readline_settings)
-            self.ioLoop.stop()
+            # https://stackoverflow.com/questions/51642267/asyncio-how-do-you-use-run-forever
+            self.ioLoop.call_soon_threadsafe(self.ioLoop.stop)
             thread.join()
     
     def do_debug(self, args):
diff --git a/test.py b/test.py
deleted file mode 100644
index 24855a6..0000000
--- a/test.py
+++ /dev/null
@@ -1,42 +0,0 @@
-import asyncio
-import threading
-import nest_asyncio
-
-
-
-async def aoutput(output):
-    print(output)
-
-# 定义一个函数来启动事件循环
-def start_event_loop(mainloop, newloop):
-    asyncio.set_event_loop(newloop)
-    newloop.run_forever()
-
-async def myinput(newloop):
-    future = asyncio.run_coroutine_threadsafe(asyncio.con(input("input")), newloop)
-    return await asyncio.wrap_future(future)
-
-async def myoutput(newloop, output):
-    future = asyncio.run_coroutine_threadsafe(aoutput(output), newloop)
-    await asyncio.wrap_future(future)
-
-async def main(mainloop, newloop):
-    while True:
-        res = await myinput(newloop)
-        await myoutput(newloop, res)
-
-if __name__ == "__main__":
-    nest_asyncio.apply()
-
-    # 创建一个新的事件循环
-    main_loop = asyncio.get_event_loop()
-    new_loop = asyncio.new_event_loop()
-
-    # 使用 threading 模块创建一个新线程,并在该线程中运行事件循环
-    thread = threading.Thread(target=start_event_loop, args=(main_loop, new_loop,))
-    thread.start()
-
-    asyncio.run(main(main_loop, new_loop))
-
-    # 等待线程完成
-    thread.join()