【发布时间】:2021-09-19 10:40:22
【问题描述】:
我正在尝试在 mcoc.py cog 中发出命令 -
@commands.command()
async def mcoc(self, ctx, tier_str:int,*,champname:str):
champname = champname.strip()
champname = champname.lower()
if champname == "bwcv":
champname=="blackwidow_timely"
url = f"https://auntm.ai/champions/{champname}/tier/{tier_str}"
session = AsyncHTMLSession()
r = await session.get(url)
await r.html.render(sleep=1, keep_page=True, scrolldown=1)
information = await r.html.find("div.sc-hiSbYr.XqbgT")
sig = await r.html.find('div.sc-fbNXWD.iFMyOV')
name = await r.html.find('div.sc-cxFLnm.izBVou')
tier_access = information[0]
self.tier = tier_access.text
async with ctx.typing:
embed = discord.Embed(title=f"{self.name_of_champ}",description=f"More Information about {self.name_of_champ} can be found (here)[{self.url_page}]")
await ctx.send(embed=embed)
但我不断收到错误-
Ignoring exception in on_command_error
Traceback (most recent call last):
File "/home/indo/.local/lib/python3.9/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "/home/indo/Documents/Python/LoB/cogs/mcoc.py", line 119, in mcoc
await r.html.render(sleep=1, keep_page=True, scrolldown=1)
File "/home/indo/.local/lib/python3.9/site-packages/requests_html.py", line 598, in render
content, result, page = self.session.loop.run_until_complete(self._async_render(url=self.url, script=script, sleep=sleep, wait=wait, content=self.html, reload=reload, scrolldown=scrolldown, timeout=timeout, keep_page=keep_page))
File "/usr/lib/python3.9/asyncio/base_events.py", line 618, in run_until_complete
self._check_running()
File "/usr/lib/python3.9/asyncio/base_events.py", line 578, in _check_running
raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/indo/.local/lib/python3.9/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: RuntimeError: This event loop is already running
即使在继续尝试之后,我仍然收到错误,这是什么原因以及如何解决这个问题?
【问题讨论】:
-
错误消息提到使用
run_until_complete。 Python 的默认 asyncio 库不允许这种类型的嵌套循环,即只能运行一个事件循环。此循环被 discord.py 占用,因此任何设置自己的循环的库都与 discord.py 不兼容。不过可能有一种解决方法。 -
发现 this question 和 this library 但由于 discord.py 和 requests_html 都是第三方软件包,我怀疑这些是否适用于您的情况。
-
@Nevus 那么我该怎么做才能让它运行呢?
-
我尝试了一个nest_asyncio,但那个没有用:(
-
它是第一个运行的东西吗?我不是专家,但似乎任何解决方案充其量都是猴子补丁。您可能必须切换库才能使其正常工作。
标签: python asynchronous discord.py runtime python-asyncio