perf: 不再需要exit来退出程序

This commit is contained in:
RockChinQ
2024-02-19 19:27:42 +08:00
parent 9a85178a29
commit 53d0059848

View File

@@ -79,18 +79,30 @@ class Application:
asyncio.create_task(self.ctrl.run())
]
async def interrupt(tasks):
await asyncio.sleep(1.5)
while await aioconsole.ainput("使用 ctrl+c 或 'exit' 退出程序 > ") != 'exit':
pass
# async def interrupt(tasks):
# await asyncio.sleep(1.5)
# while await aioconsole.ainput("使用 ctrl+c 或 'exit' 退出程序 > ") != 'exit':
# pass
# for task in tasks:
# task.cancel()
# await interrupt(tasks)
import signal
def signal_handler(sig, frame):
for task in tasks:
task.cancel()
self.logger.info("程序退出.")
exit(0)
await interrupt(tasks)
signal.signal(signal.SIGINT, signal_handler)
await asyncio.gather(*tasks, return_exceptions=True)
except asyncio.CancelledError:
pass
except Exception as e:
self.logger.error(f"应用运行致命异常: {e}")
self.logger.debug(f"Traceback: {traceback.format_exc()}")
self.logger.info("程序退出.")