mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-02 03:55:55 +00:00
perf: 不再需要exit来退出程序
This commit is contained in:
@@ -79,18 +79,30 @@ class Application:
|
|||||||
asyncio.create_task(self.ctrl.run())
|
asyncio.create_task(self.ctrl.run())
|
||||||
]
|
]
|
||||||
|
|
||||||
async def interrupt(tasks):
|
# async def interrupt(tasks):
|
||||||
await asyncio.sleep(1.5)
|
# await asyncio.sleep(1.5)
|
||||||
while await aioconsole.ainput("使用 ctrl+c 或 'exit' 退出程序 > ") != 'exit':
|
# while await aioconsole.ainput("使用 ctrl+c 或 'exit' 退出程序 > ") != 'exit':
|
||||||
pass
|
# pass
|
||||||
|
# for task in tasks:
|
||||||
|
# task.cancel()
|
||||||
|
|
||||||
|
# await interrupt(tasks)
|
||||||
|
|
||||||
|
import signal
|
||||||
|
|
||||||
|
def signal_handler(sig, frame):
|
||||||
for task in tasks:
|
for task in tasks:
|
||||||
task.cancel()
|
task.cancel()
|
||||||
|
self.logger.info("程序退出.")
|
||||||
await interrupt(tasks)
|
exit(0)
|
||||||
|
|
||||||
|
signal.signal(signal.SIGINT, signal_handler)
|
||||||
|
|
||||||
|
await asyncio.gather(*tasks, return_exceptions=True)
|
||||||
|
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
pass
|
pass
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.error(f"应用运行致命异常: {e}")
|
self.logger.error(f"应用运行致命异常: {e}")
|
||||||
self.logger.debug(f"Traceback: {traceback.format_exc()}")
|
self.logger.debug(f"Traceback: {traceback.format_exc()}")
|
||||||
self.logger.info("程序退出.")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user