Issue
i have a weird error and can't find a fix for it. I'm searching since weeks for a solution, I found a few other stackoverflow articles about it, but sadly they doesn't helped me. Aside from this, I can't find many articles in google about my problem. I even tried to switch MySQL drivers to avoid this error, but it doesn't help. It's really frustrating, because it looks like there's no reason for this error.
I tried aiomysql, asyncmy and even peewee-async. I'm using Python 3.8 on Linux Debian 10. And I found a Pull Request that "should" help to solve this error for aiomysql, but it was never finished/published/whatever.
So everytime I build a async mysql connection, execute a few queries and try to close the connection after it, I still get everytime the error An open stream object is being garbage collected; call "stream.close()" explicitly
So first of all, that's my function to start the async connection:
async def getConnection():
mydb = await peewee_async.aiomysql.connect(
host="123.123.123",
user="admin",
password="123",
db="mydatabase")
return mydb
And here is an example code, where the connection closing doesn't work.
@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()
Like you can see, I closed the connection at the end but still gets the error, everytime the event runs. I have that in other events too and really don't know why. Aside from that, I don't get the big error stacktrace, only this one-line error.
Solution
Problem is solved. I needed to update Python3.8 to Python3.9.
Answered By - Razzer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.