为什么会出现“asyncio.exceptions.CancelledError”错误?

问题描述 投票:0回答:1

昨天之前程序完成时没有错误,但今天每个程序终止后都会出现此错误:

Traceback (most recent call last):
  File "C:\Users\ALEXE\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ALEXE\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\Users\ALEXE\PycharmProjects\MY-TELEGRAM-BOT\BOT\MAIN.py", line 437, in main
    await dp.start_polling(bot)
  File "C:\Users\ALEXE\PycharmProjects\MY-TELEGRAM-BOT\venv\Lib\site-packages\aiogram\dispatcher\dispatcher.py", line 543, in start_polling
    done, pending = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ALEXE\AppData\Local\Programs\Python\Python311\Lib\asyncio\tasks.py", line 418, in wait
    return await _wait(fs, timeout, return_when, loop)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ALEXE\AppData\Local\Programs\Python\Python311\Lib\asyncio\tasks.py", line 525, in _wait
    await waiter
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\ALEXE\PycharmProjects\MY-TELEGRAM-BOT\BOT\MAIN.py", line 442, in <module>
    asyncio.run(main())
  File "C:\Users\ALEXE\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\ALEXE\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 123, in run
    raise KeyboardInterrupt()
KeyboardInterrupt

Process finished with exit code -1073741510 (0xC000013A: interrupted by Ctrl+C)

我没有用ctrl+c组合键结束程序,我只是按停止按钮。

也许我错误地终止了会话?我只是不知道为什么会出现这个错误,因为它以前不存在。

async def main():
    try:
        await client.start()
        await client.connect()
        await dp.start_polling(bot)
    finally:
        await client.disconnect()

if __name__ == "__main__":
    asyncio.run(main())

如果有任何帮助或建议,我将不胜感激。

python python-asyncio telegram-bot aiohttp
1个回答
0
投票

您只需在 main() 中捕获 CancelledError 即可毫无错误地终止机器人。 文档

© www.soinside.com 2019 - 2024. All rights reserved.