为什么我得到 "An open stream object is being garbage collected; call "stream.close()" 显式"?

Why i'm getting "An open stream object is being garbage collected; call "stream.close()" explicitly"?

我有一个奇怪的错误,找不到解决方法。几周以来我一直在寻找解决方案,我发现了其他一些关于它的 Whosebug 文章,但遗憾的是它们没有帮助我。除此之外,我在 google 中找不到很多关于我的问题的文章。我什至试图切换 MySQL 驱动程序来避免这个错误,但它没有帮助。真令人沮丧,因为看起来没有理由出现这个错误。

我试过了aiomysql, asyncmy and even peewee-async。我在 Linux Debian 10 上使用 Python 3.8。 我发现 Pull Request“应该”有助于解决 aiomysql 的这个错误,但从来没有 finished/published/whatever。

所以每次我建立一个异步 mysql 连接,执行一些查询并尝试在它之后关闭连接时,每次我仍然得到错误 An open stream object is being garbage collected; call "stream.close()" explicitly

首先,这是我启动异步连接的功能:

async def getConnection():
    mydb = await peewee_async.aiomysql.connect(
        host="123.123.123",
        user="admin",
        password="123",
        db="mydatabase")
    return mydb

这是一个示例代码,其中连接关闭不起作用。

@commands.Cog.listener("on_message")
async def on_message(self, message):

    mydb = await getConnection()
    mycursor = await mydb.cursor()
    if len(message.mentions) >= 1 and message.author not in message.mentions and not message.author.bot:
        await mycursor.execute("SELECT * FROM afk WHERE memberid = %s", (int(message.mentions[0].id),))
        myresult = await mycursor.fetchone()
        if myresult and ".afk" not in message.content:
            status = myresult[1]
            time1 = datetime.fromtimestamp(int(myresult[2]))
            diff = datetime.now() - time1

            if (diff >= timedelta(minutes=2)) and (diff < timedelta(minutes=60)):
                zeit = strfdelta(diff, "{minutes} Minuten")

            elif (diff >= timedelta(minutes=60)) and (diff < timedelta(minutes=120)):
                zeit = strfdelta(diff, "{hours} Stunde und {minutes} Minuten")

            elif (diff >= timedelta(minutes=120)) and (diff < timedelta(minutes=1440)):
                zeit = strfdelta(diff, "{hours} Stunden und {minutes} Minuten")

            elif (diff >= timedelta(minutes=1440)) and (diff < timedelta(minutes=2880)):
                zeit = strfdelta(diff, "{days} Tag und {hours} Stunden")

            elif diff >= timedelta(minutes=2880):
                zeit = strfdelta(diff, "{days} Tage und {hours} Stunden")

            else:
                zeit = "Eben gerade"

            await mycursor.close()
            mydb.close()
            return

    await mycursor.execute("SELECT * FROM afk WHERE memberid = %s", (int(message.author.id),))
    myresult1 = await mycursor.fetchone()
    # React on AFK User writes
    if myresult1 and ".afk" not in message.content:
        await mycursor.execute("DELETE FROM afk WHERE memberid = %s", (int(message.author.id),))
        if message.author.nick is None:
            updated = message.author.name.replace("AFK | ", "")
        else:
            updated = message.author.nick.replace("AFK | ", "")
        if not int(message.author.id) == int(message.guild.owner.id):
            await message.author.edit(nick=updated, reason="AFK-Status entfernt")

        await mydb.commit()
        await mycursor.close()
        mydb.close()
        return

    await mycursor.close()
    mydb.close()

如您所见,我在最后关闭了连接,但每次事件运行时仍然出现错误。我在其他事件中也有,真的不知道为什么。除此之外,我没有得到大错误堆栈跟踪,只有这一行错误。

问题已解决。我需要将 Python3.8 更新为 Python3.9.