【问题标题】:RuntimeWarning: coroutine 'BaseClient.read_output' was never awaitedRuntimeWarning:从未等待协程“BaseClient.read_output”
【发布时间】:2021-10-15 20:52:21
【问题描述】:

由于某种原因,当我尝试使用 PyPresence 设置与我的 Xbox Live 状态相同的 Discord 丰富状态时,但我一直收到错误消息 RuntimeWarning: coroutine 'BaseClient.read_output' was never awaited

这是我的代码

import os
import asyncio
from aiohttp import ClientSession
from xbox.webapi.api.client import XboxLiveClient
from xbox.webapi.authentication.manager import AuthenticationManager
from xbox.webapi.authentication.models import OAuth2TokenResponse

from pypresence import Presence
import requests
import time

clientID = os.environ.get("CLIENT_ID")
clientSecret = os.environ.get("CLIENT_SECRET")

discordAppID = os.environ.get("DISCORD_APP_ID")
rpc = Presence(discordAppID)
rpc.connect()

async def async_main():
    tokens_file = "./tokens.json"
    async with ClientSession() as session:
        authManager = AuthenticationManager(session, clientID, clientSecret, "")
        with open(tokens_file, mode = "r") as f:
              token = f.read()
        authManager.oauth = OAuth2TokenResponse.parse_raw(token)

        xblClient = XboxLiveClient(authManager)

        rpc.update(state = "test state")

asyncio.run(async_main())

当我取出rpc.update(state="test state") 行时不会发生错误,所以我知道这是错误的原因,但我无法弄清楚为什么会导致错误以及如何修复它。

编辑:这也是RuntimeError: Cannot run the event loop while another loop is running

【问题讨论】:

    标签: python async-await discord


    【解决方案1】:

    好的,所以我可以通过安装一个名为 nest_asyncio 的包来修复错误,然后在我的代码开头添加以下内容:

    import nest_asyncio
    nest_asyncio.apply()
    

    【讨论】:

      猜你喜欢
      • 2018-10-26
      • 2021-10-22
      • 2019-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-10
      • 2020-10-10
      • 2022-01-20
      相关资源
      最近更新 更多