How work with tasks.loop and sqlite3 in discord.py

Viewed 46

When I process some data in Cogs and in this job. The cycle fails because the data is not synchronized and writes that the database is locked. I understand the problem that prevents working with the bot in the future, but with a large flow of people, an error still pops up

@tasks.loop(seconds = 1)
async def secondLoop():
    games = cursor.execute(f"SELECT players, joinedTimePlayers, channelId, id FROM games WHERE onPick = 0").fetchall()
    guild = bot.guilds[0]

    for game in games:
        playersTime = game[1].split()
        players = game[0].split()

        if len(playersTime) > 0:
            for playerTimeIndex in range(len(playersTime)):
                try:
                    if int(playersTime[playerTimeIndex]) > 1:
                        playersTime[playerTimeIndex] = str(int(playersTime[playerTimeIndex]) - 1)
                        cursor.execute(f"UPDATE games SET joinedTimePlayers = '{nl.join(playersTime)}' WHERE id = {game[3]}")
                        connection.commit()

                    elif int(playersTime[playerTimeIndex]) == 1:
                        channel = bot.get_channel(game[2])
                        embedToChannel =  discord.Embed(description = f"Пользователь <@!{players[playerTimeIndex]}> автоматически вышел из лобби, по причине бездействия", colour = discord.Color.red())
                        await channel.send(embed = embedToChannel)

                        playersTime[playerTimeIndex] = 0
                        players.remove(players[playerTimeIndex])
                        playersTime.remove(playersTime[playerTimeIndex])

                        cursor.execute(f"UPDATE games SET joinedTimePlayers = '{nl.join(playersTime)}', players = '{nl.join(players)}' WHERE id = {game[3]}")
                        connection.commit()
                except Exception as e:
                    pass

0 Answers
Related