From 041a2191513f1cdb82d8aff3e2001b8e84a91ca0 Mon Sep 17 00:00:00 2001 From: snowy Date: Wed, 24 Jul 2024 02:54:59 +0800 Subject: [PATCH] =?UTF-8?q?:zap:=20=E6=B7=BB=E5=8A=A0=E8=BF=9B=E7=A8=8B?= =?UTF-8?q?=E5=8F=8A=E7=94=9F=E5=91=BD=E5=91=A8=E6=9C=9F=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E5=99=A8=EF=BC=8C=E6=B7=BB=E5=8A=A0=E8=BD=BB=E9=9B=AA=E6=A1=86?= =?UTF-8?q?=E6=9E=B6=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- liteyuki/bot/__init__.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/liteyuki/bot/__init__.py b/liteyuki/bot/__init__.py index 889ada0a..02eaf2d5 100644 --- a/liteyuki/bot/__init__.py +++ b/liteyuki/bot/__init__.py @@ -105,6 +105,34 @@ $$$$$$$$/ $$$$$$/ $$/ $$$$$$$$/ $$/ $$$$$$/ $$/ $$/ $$$$$$/ else: should_exit = True + @staticmethod + def _run_coroutine(*coro: Coroutine): + """ + 运行协程 + Args: + coro: + + Returns: + + """ + # 检测是否有现有的事件循环 + new_loop = False + try: + loop = asyncio.get_event_loop() + except RuntimeError: + new_loop = True + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + + if new_loop: + for c in coro: + loop.run_until_complete(c) + loop.close() + + else: + for c in coro: + loop.create_task(c) + @property def status(self) -> int: """ @@ -123,9 +151,9 @@ $$$$$$$$/ $$$$$$/ $$/ $$$$$$$$/ $$/ $$$$$$/ $$/ $$/ $$$$$$/ logger.info("Stopping LiteyukiBot...") logger.debug("Running before_restart functions...") - asyncio.run(self.lifespan.before_restart()) + self._run_coroutine(self.lifespan.before_restart()) logger.debug("Running before_shutdown functions...") - asyncio.run(self.lifespan.before_shutdown()) + self._run_coroutine(self.lifespan.before_shutdown()) ProcessingManager.restart() self.running = False