asyncio.run RuntimeError:事件循环已关闭

问题描述 投票:0回答:1
import robloxapi, asyncio
client = robloxapi.Client(".ROBLOSECURITY Cookie Here") # Removed this for security reasons

async def main():
    user = await client.get_self()

    try:
        role = await user.get_role_in_group(1)
    except robloxapi.utils.errors.NotFound:
        role = None

    if role:    
        print(role.name)
    else:
        print("Not in group")

asyncio.run(main())

此代码引发RuntimeError: Event loop is closed,但我不知道为什么,

我尝试用此替换asyncio.run

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()

但是它给了我同样的错误

python runtime-error python-asyncio roblox event-loop
1个回答
0
投票

我是StackOverflow的新手,我无法写评论。我会在这里写答案。我在异步处理中遇到了类似的问题。

所以我所遇到的问题通过更改使用的Eventloop python得以解决。

在低于3.8-的python版本中:-在Windows上使用SelectorEventLoop-ProactorEventLoop在Linux上使用。

((在python 3.8+中,它们都是ProactorEventLoop)所以如果您安装了python 3.8+,这将无济于事。因为您将在Windows上拥有与WSL相同的eventloop。

如果您的Python版本低于3.8,则可能会有所帮助。

因此您可以尝试手动设置在您使用WSL时也使用ProactorEventLoop夹

asyncio.set_event_loop(asyncio.ProactorEventLoop())

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()

希望此信息对您有帮助。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.