aiocqhttp
aiocqhttp copied to clipboard
无法Ctrl C结束HTTP服务器
如题 环境为Windows11 + python3.10.2
无法Ctrl C,只能关闭终端窗口并重新打开,有点麻烦
試一試反复 CtrlC。我是這樣的
On Sun., Nov. 20, 2022, 20:36 PSoul, @.***> wrote:
如题 环境为Windows11 + python3.10.2
无法Ctrl C,只能关闭终端窗口并重新打开,有点麻烦
— Reply to this email directly, view it on GitHub https://github.com/nonebot/aiocqhttp/issues/67, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADKGVB5QPMJNH27Y7ZCUAXDWJLGYJANCNFSM6AAAAAASGEMGNY . You are receiving this because you are subscribed to this thread.Message ID: @.***>
試一試反复 CtrlC。我是這樣的 … On Sun., Nov. 20, 2022, 20:36 PSoul, @.> wrote: 如题 环境为Windows11 + python3.10.2 无法Ctrl C,只能关闭终端窗口并重新打开,有点麻烦 — Reply to this email directly, view it on GitHub <#67>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADKGVB5QPMJNH27Y7ZCUAXDWJLGYJANCNFSM6AAAAAASGEMGNY . You are receiving this because you are subscribed to this thread.Message ID: @.>
不行,看来各位都有这个问题??
找到了一個簡單的辦法.
可以使用 aioconsole,而後在執行 cqhttp 的同時再讀取用戶輸入就好了:
import asyncio
from aiocqhttp import CQHttp
from aioconsole import ainput
async def interrupter():
while await ainput() != 'exit':
pass
exit()
async def main():
bot = CQHttp()
await asyncio.gather(bot.run_task(), interrupter())
asyncio.run(main())
鍵盤上鍵入 exit
再 enter 就可以退出了.
这个问题是由于hypercorn框架内部注册了 signal.SIGINT 信号导致的:
https://github.com/pgjones/hypercorn/blob/3fbd5f245e5dfeaba6ad852d9135d6a32b228d05/src/hypercorn/asyncio/run.py#L64-L76
为了使web server能在ctrl+c时才退出,当quart开始运行时,默认会为底层忽略传入 shutdown_trigger ,以使hypercorn自动监听 退出信号。 但如果你的程序自行控制了整个程序的退出,同时以一种较为动态的方式运行了aiocqhttp(quart),就会导致 ctrl+c 时仍有 task 在运行而无法退出。
简易的解决办法
定义一个阻塞的异步方法:
async def shutdown_trigger_placeholder():
while True:
await asyncio.sleep(1)
作为shutdown_trigger 具名参数传入 CQHttp 的run方法:
bot.run(
...
shutdown_trigger=shutdown_trigger_placeholder
)