python - 命令引发异常: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
解决方案
推荐阅读
- node.js - NextJS 组件级 SASS 样式
- flutter - Flutter Chopper 文件上传 API 抛出 NoSuchMethodError
- python - Numpy 从字符串解析到 ndarray
- azure - 使用 Jenkins 将 Terraform (IaC) 部署到 Azure
- php - 如何取消设置或使条件为真,如果传递的变量为空或 null
- r - 在 Shiny 应用程序中的 Divs 上生成不同的可点击事件
- swift - 快速减少(调整)双精度/浮点变量小数点
- django - 在 Django modelformset_factory 中传递模型实例的 id
- python - 为什么python默认启用断言
- oracle - 在 oracle sql 中从子节点中查找根父节点