我该如何修复任务已被破坏但仍处于待处理状态?

Raz*_*zer 7 python mysql task python-asyncio pycord

我有个问题。因此,每次用户在我的不和谐服务器上写入聊天消息时,我都会运行一个任务 - 它称为on_message. 所以我的机器人在这个事件中有很多事情要做,我经常遇到这种错误:

\n
Task was destroyed but it is pending!\ntask: <Task pending name='pycord: on_message' coro=<Client._run_event() done, defined at /Bots/gift-bot/discord/client.py:374> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f68a7bdfc10>()]>>\n
Run Code Online (Sandbox Code Playgroud)\n

所以我想如果我想解决这个问题,我需要加速我的代码。但遗憾的是,我不知道如何修复此错误。

\n

编辑:我集成了计时,这就是我打印的内容:

\n
Task was destroyed but it is pending!\ntask: <Task pending name='pycord: on_message' coro=<Client._run_event() done, defined at /Bots/gift-bot/discord/client.py:374> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f01063f98e0>()]>>\n2 if checks done - 7.867813110351562e-06\n5 if checks done - 0.0061550140380859375\nmysql checks done - 0.010785341262817383\ntask done - 0.13075661659240723\n2 if checks done - 8.344650268554688e-06\n5 if checks done - 0.011545896530151367\nmysql checks done - 0.02138519287109375\ntask done - 0.11132025718688965\n2 if checks done - 2.0503997802734375e-05\n5 if checks done - 0.008122920989990234\nmysql checks done - 0.012276411056518555\n2 if checks done - 1.0728836059570312e-05\n5 if checks done - 0.014346837997436523\nmysql checks done - 0.040288448333740234\ntask done - 0.12520265579223633\n2 if checks done - 1.0728836059570312e-05\n5 if checks done - 0.0077972412109375\nmysql checks done - 0.013320684432983398\ntask done - 0.1502058506011963\ntask done - 0.10663175582885742\n2 if checks done - 9.775161743164062e-06\n5 if checks done - 0.006486177444458008\nmysql checks done - 0.011229515075683594\nTask was destroyed but it is pending!\ntask: <Task pending name='pycord: on_message' coro=<Client._run_event() done, defined at /Bots/gift-bot/discord/client.py:374> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f010609a9d0>()]>>\n2 if checks done - 6.67572021484375e-06\n5 if checks done - 0.0049741268157958984\nmysql checks done - 0.008575677871704102\ntask done - 0.10633635520935059\n
Run Code Online (Sandbox Code Playgroud)\n

这是集成计时的代码:

\n
    @commands.Cog.listener("on_message")\n    async def on_message(self, message):\n        start = time.time()\n\n        # Check ob Nachricht gez\xc3\xa4hlt werden kann\n\n\n        if message.author.bot:\n            return\n\n        if message.type != discord.MessageType.default:\n            return\n            \n        print(f"2 if checks done - {time.time() - start}")\n\n        if isinstance(message.channel, discord.channel.DMChannel):\n            return await message.reply(f'Hey {message.author.name}!\\nLeider bin ich der falsche Ansprechpartner, falls du Hilfe suchst.. <:pepe_hands:705896495601287320>\\nBetrete den https://discord.gg/deutschland Bl4cklist-Discord und sende unserem Support-Bot <@671421220566204446> (`Bl4cklistSupport#7717`) eine Private-Nachricht, damit sich unser Support-Team um dein Problem so schnell es geht k\xc3\xbcmmern kann. <:pepe_love:759741232443949107>')\n\n        # ENTFERNEN AM 30. APRIL\n        prefix_now = await get_prefix(message)\n        if message.content.startswith(str(prefix_now)):\n            try:\n                await message.reply("\xe2\x80\xba <a:alarm:769215249261789185> - **UMSTIEG AUF SLASH-COMMANDS:** Ab **jetzt** laufen alle Befehle dieses Bots auf `/` - um Leistung zu sparen und die Erfahrung zu verbessern. Nutze `/help` um eine Befehlsliste zu sehen.")\n            except discord.Forbidden:\n                pass\n            return\n\n        if self.client.user in message.mentions:\n\n                response = choice([\n                "Mit mir kann man die coolsten Gewinnspiele starten! <a:gift:843914342835421185>",\n                'Wird Zeit jemanden den Tag zu vers\xc3\xbc\xc3\x9fen! <:smile:774755282618286101>',\n                "Wer nicht auf diesem Server ist, hat die Kontrolle \xc3\xbcber sein Leben verloren! <a:lach_blue2:803693710490861608>",\n                "Wann startet endlich ein neues Gewinnspiel? <:whut:848347703217487912>",\n                "Ich bin der BESTE Gewinnspiel-Bot - Wer was anderes sagt, l\xc3\xbcgt! <:wyldekatze:842157727169773608>"\n                ])\n\n                try:\n                    await message.reply(f"{response} (Mein Pr\xc3\xa4fix: `/`)", mention_author=False)\n                except (discord.Forbidden, discord.HTTPException, discord.NotFound):\n                    pass\n                return\n                \n        print(f"5 if checks done - {time.time() - start}")\n\n\n        # Cooldown\n\n\n        #self.member_cooldown_list = [i for i in self.member_cooldown_list if i[1] + self.cooldown_val > int(time.time())]\n        #member_index = next((i for i, v in enumerate(self.member_cooldown_list) if v[0] == message.author.id), None)\n        #if member_index is not None:\n        #    if self.member_cooldown_list[member_index][1] + self.cooldown_val > int(time.time()):\n        #        return\n\n        #self.member_cooldown_list.append((message.author.id, int(time.time())))\n\n\n        # Rollen-Check (Bonus/Ignore)\n\n\n        count = 1\n        mydb = await getConnection()\n        mycursor = await mydb.cursor()\n        await mycursor.execute("SELECT ignore_role_id, bonus_role_id FROM guild_role_settings WHERE guild_id = %s", (message.author.guild.id,))\n        in_database = await mycursor.fetchone()\n        if in_database:\n            if in_database[0] is not None:\n                role_list = in_database[0].split(" ")\n                for roleid in role_list:\n                    try:\n                        int(roleid)\n                    except ValueError:\n                        continue\n\n                    role = message.author.guild.get_role(int(roleid))\n                    if role is None:\n                        continue\n\n                    if role in message.author.roles:\n                        await mycursor.close()\n                        mydb.close()\n                        return\n\n            if in_database[1] is not None:\n                role_list = in_database[1].split(" ")\n                for roleid in role_list:\n                    try:\n                        int(roleid)\n                    except ValueError:\n                        continue\n\n                    role = message.author.guild.get_role(int(roleid))\n                    if role is None:\n                        continue\n\n                    if role in message.author.roles:\n                        count += 1\n\n\n        # Kanal-Check (Bonus/Ignore)\n\n\n        await mycursor.execute("SELECT ignore_channel_id FROM guild_channel_settings WHERE guild_id = %s", (message.author.guild.id,))\n        in_database1 = await mycursor.fetchone()\n        if in_database1:\n            if in_database1[0] is not None:\n                channel_list = in_database1[0].split(" ")\n                for channelid in channel_list:\n\n                    try:\n                        int(channelid)\n                    except ValueError:\n                        continue\n\n                    if int(message.channel.id) == int(channelid):\n                        await mycursor.close()\n                        mydb.close()\n                        return\n                        \n        print(f"mysql checks done - {time.time() - start}")\n\n\n        # In Datenbank eintragen\n\n        await mycursor.execute("SELECT * FROM guild_message_count WHERE guild_id = %s AND user_id = %s",\n                               (message.author.guild.id, message.author.id))\n        in_database2 = await mycursor.fetchone()\n        if in_database2:\n            await mycursor.execute(\n                "UPDATE guild_message_count SET user_id = %s, message_count = message_count + %s WHERE guild_id = %s AND user_id = %s",\n                (message.author.id, count, message.author.guild.id, message.author.id))\n        else:\n            await mycursor.execute(\n                "INSERT INTO guild_message_count (user_id, message_count, guild_id) VALUES (%s, %s, %s)",\n                (message.author.id, count, message.author.guild.id))\n\n        await mydb.commit()\n        await mycursor.close()\n        mydb.close()\n        \n        print(f"task done - {time.time() - start}")\n
Run Code Online (Sandbox Code Playgroud)\n

如果我尝试启动我的机器人,asyncio.run(client.start('token'))我会多次收到此错误:

\n
Ignoring exception in on_guild_channel_delete\nTraceback (most recent call last):\n  File "/Bots/gift-bot/discord/client.py", line 382, in _run_event\n    await coro(*args, **kwargs)\n  File "/Bots/gift-bot/cogs/misc_events.py", line 738, in on_guild_channel_delete\n    await self.client.wait_until_ready()\n  File "/Bots/gift-bot/discord/client.py", line 978, in wait_until_ready\n    await self._ready.wait()\n  File "/usr/local/lib/python3.9/asyncio/locks.py", line 226, in wait\n    await fut\nRuntimeError: Task <Task pending name='pycord: on_guild_channel_delete' coro=<Client._run_event() running at /Bots/gift-bot/discord/client.py:382>> got Future <Future pending> attached to a different loop\n
Run Code Online (Sandbox Code Playgroud)\n

我在 Debian 10 vServer 上使用 Python3.9 和 pycord2.0.0b5。

\n

Niz*_*med 5

await表达式会阻塞包含的协程,直到awaited 可等待返回。这阻碍了协程的进展。但await在协程中必须将控制权交还给事件循环,以便其他协程可以继续进行。

太多await可能会出现问题,只会让进度变慢。
on_message通过将协程方法分解为子任务来重构它。

async def _check_channel(self, message, pool):
    async with pool.acquire() as conn:
        async with conn.cursor() as cursor:
            await cursor.execute(
                "SELECT ignore_channel_id FROM guild_channel_settings WHERE guild_id = %s",
                (message.author.guild.id,),
            )
            in_database = await cursor.fetchone()

    if in_database and in_database[0] is not None:
        channel_list = in_database[0].split(" ")
        for channelid in channel_list:

            try:
                channel_id_int = int(channelid)
            except ValueError:
                continue

            if int(message.channel.id) == channel_id_int:
                return False


async def _get_role_count(self, message, pool):
    async with pool.acquire() as conn:
        async with conn.cursor() as cursor:
            await cursor.execute(
                "SELECT ignore_role_id, bonus_role_id FROM guild_role_settings WHERE guild_id = %s",
                (message.author.guild.id,),
            )
            in_database = await cursor.fetchone()
    if in_database:
        first_item, second_item, *_ = in_database
        if first_item is not None:
            role_list = first_item.split(" ")
            for roleid in role_list:
                try:
                    roleid_int = int(roleid)
                except ValueError:
                    continue

                role = message.author.guild.get_role(roleid_int)
                if role is None:
                    continue
                if role in message.author.roles:
                    return False

        if second_item is not None:
            role_list = second_item.split(" ")
            count = 0
            for roleid in role_list:
                try:
                    roleid_int = int(roleid)
                except ValueError:
                    continue

                role = message.author.guild.get_role(roleid_int)
                if role is None:
                    continue
                if role in message.author.roles:
                    count += 1
            return count


@commands.Cog.listener("on_message")
async def on_message(self, message):
    if message.author.bot:
        return
    if message.type != discord.MessageType.default:
        return
    if isinstance(message.channel, discord.channel.DMChannel):
        return

    # Cooldown

    self.member_cooldown_list = [
        i
        for i in self.member_cooldown_list
        if i[1] + self.cooldown_val > int(time.time())
    ]
    member_index = next(
        (
            i
            for i, v in enumerate(self.member_cooldown_list)
            if v[0] == message.author.id
        ),
        None,
    )
    if member_index is not None:
        if self.member_cooldown_list[member_index][1] + self.cooldown_val > int(
            time.time()
        ):
            return

    self.member_cooldown_list.append((message.author.id, int(time.time())))

    loop = asyncio.get_running_loop()
    db_pool = await aiomysql.create_pool(
        minsize=3,
        host="<host>",
        port=3306,
        user="<user>",
        password="<password>",
        db="<db_name>",
        autocommit=False,
        loop=loop,
    )
    count = 1

    check_channel_task = asyncio.create_task(
        self._check_channel(self, message, db_pool)
    )
    role_count_task = asyncio.create_task(self._get_role_count(self, message, db_pool))

    # write to database

    mydb = await db_pool.acquire()
    mycursor = await mydb.cursor()
    await mycursor.execute(
        "SELECT * FROM guild_message_count WHERE guild_id = %s AND user_id = %s",
        (message.author.guild.id, message.author.id),
    )
    in_database = await mycursor.fetchone()

    role_count = await role_count_task
    check_channel = await check_channel_task
    if False in (role_count, check_channel):
        await mycursor.close()
        db_pool.release(mydb)
        db_pool.close()
        await db_pool.wait_closed()
        return
    if role_count:
        count += role_count
    if in_database:
        await mycursor.execute(
            "INSERT INTO guild_message_count (user_id, message_count, guild_id) VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE message_count = message_count + 1",
            (message.author.id, count, message.author.guild.id),
        )

    await mydb.commit()
    await mycursor.close()
    db_pool.release(mydb)
    db_pool.close()
    await db_pool.wait_closed()
Run Code Online (Sandbox Code Playgroud)

我使用部分方法中的代码创建了两个私有异步方法,on_message以使进度并发。虽然on_message在 中被阻止await,但重构的方法可能会独立于on_message方法而进行。为了实现这一点,我从两个新的协程中创建了两个任务。asyncio.create_tasks安排要运行的任务,无需await. 一旦on_message将控制权交回给任何await后续任务创建的事件循环,这些任务就可以运行。

我没有运行代码。这是最好的努力。您必须尝试通过移动await任务所在的块来进行试验。并且还运行它client.run以避免Future 附加到不同的循环错误。


Raz*_*zer 0

对于所有遇到相同错误消息的人来说 - 这是一个修复程序,您可以尝试在一年后发挥作用(至少对我来说):

  • 尝试将代码部分移至异步函数中并仅调用该函数,特别是如果您将来可能再次需要它们或者它们花费大量时间 - 这在大多数情况下解决了我的错误。