首页 > 解决方案 > 命令引发异常:RuntimeError: asyncio.run() 无法从正在运行的事件循环中调用

问题描述

我正在制作一个不和谐的机器人,问题是当我向服务器发送歌词时,机器人停止工作并且不接受任何命令,直到它停止给我歌词。所以我想利用,multithreading但它给了我错误。这是代码

average_time = []
search_message = ''


async def lyric(ctx, artist: str, title):
    global search_message, average_time
    start = time.perf_counter()
    search_message = await ctx.send(':mag: Searching lyrics, you\'ll be notified once it\'s done!')
    provided = discord.Embed(title="Genius",
                             description="This lyrics were provided by **Genius**.\nGenius is the home of Music lyrics and knowledge. "
                             "If you wish to add Genius to your bot or to work with it, __[here](https://genius.com/developers)__ is the website you can visit.",
                             url='https://www.genius.com')
    lyrc = f'{genius.search_song(title=title, artist=artist).lyrics}'
    total = time.perf_counter() - start
    average_time.append(total)
    await ctx.send(
        f"Hey, {ctx.author.mention}\nYou searched this **{total:1.2f}** seconds ago.")
    # This is a function on the global variable that split the lyrics
    # It is capable of sending 3986 words by splitting them
    temp, temp2 = lyric_spliter(lyrc)
    if temp != '':
        await ctx.send(f'```{temp}```')
    if temp2 != '':
        await ctx.send(f'```{temp2}```')
    await search_message.edit(delete_after=0)
    await ctx.send(embed=provided)


@client.command()
async def lyrics(ctx, artist: str, *, title):
    async def main():
        loop = asyncio.get_running_loop()
        with concurrent.futures.ThreadPoolExecutor() as p:
            await loop.run_in_executor(p, lyric, args=[ctx, artist, title])
    asyncio.run(main())

当我运行它时,它给了我这个错误

Command raised an exception: RuntimeError: asyncio.run() cannot be called from a running event loop

标签: pythonpython-3.xmultithreadingpython-asyncio

解决方案


推荐阅读