我有一个非常奇怪的问题。目前我尝试打开与 MySQL 的数据库连接,如果用户写了一条消息,则获取一些东西并关闭它(使用库 aiomysql)。所以一切正常,除了关闭连接,我不明白为什么。因为我使用这种方式来关闭我所有其他不和谐机器人中的连接并且它工作完美,但不是在这里。
我的代码:
@commands.Cog.listener("on_message")
async def on_message(self, message):
###### React on AFK User ping ######
mydb1 = await getConnection1()
mycursor = await mydb1.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:
..
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:
..
print(1)
await mycursor.close()
mydb1.close()
print(2)
我得到错误An open stream object is being garbage collected; call "stream.close()" explicitly.
- 为什么?最后一个连接关闭。我正在使用aiomysql
.