Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

无法Ctrl C结束HTTP服务器 #67

Open
PSoul opened this issue Nov 21, 2022 · 4 comments
Open

无法Ctrl C结束HTTP服务器 #67

PSoul opened this issue Nov 21, 2022 · 4 comments

Comments

@PSoul
Copy link

PSoul commented Nov 21, 2022

如题
环境为Windows11 + python3.10.2

无法Ctrl C,只能关闭终端窗口并重新打开,有点麻烦

@cleoold
Copy link
Member

cleoold commented Nov 21, 2022 via email

@PSoul
Copy link
Author

PSoul commented Nov 22, 2022

試一試反复 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: @.>

不行,看来各位都有这个问题??

@cleoold
Copy link
Member

cleoold commented Jun 11, 2023

找到了一個簡單的辦法.

可以使用 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 就可以退出了.

@RockChinQ
Copy link

RockChinQ commented Feb 15, 2024

这个问题是由于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
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants