1

I am trying to make a command in 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)

But continuesly I am getting the error-

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

Even after continues attempts, I am still getting the error, What's the reason and how can I fix this?

5
  • The error message mentions use of run_until_complete. Python's default asyncio library doesn't allow this type of nested loops ie only one event loop can run. This loop is occupied by discord.py so any library setting up its own loop is incompatible with discord.py. There might be a workaround though. Commented Sep 19, 2021 at 11:53
  • Found this question and this library but since both discord.py and requests_html are third party packages, I doubt these would apply to your case. Commented Sep 19, 2021 at 11:56
  • @Nevus So what can I do to get it running? Commented Sep 19, 2021 at 12:51
  • I tried the nest_asyncio one but that one didn't worked :( Commented Sep 19, 2021 at 12:54
  • Is it the first thing that runs? I am no expert but it seems that any solution would be monkeypatching at best. You might have to switch libraries to make it work. Commented Sep 19, 2021 at 14:01

1 Answer 1

0

To resolve the error <RuntimeError: This event loop is already running> , which seems to be specific for Jupyter Notebook and Jupyter Lab, do this:

pip install nest_asyncio

you may also need this in your python app

import nest_asyncio
nest_asyncio.apply()

For complete patch info refence:

https://github.com/psf/requests-html/issues/402

https://github.com/erdewit/nest_asyncio

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.