Discord bot encounters RuntimeError: no running event loop while initiating asyncio coroutine task

I’m developing a Discord bot that regularly fetches stock prices and publishes them to different channels. Everything works properly except that I run into a RuntimeError when trying to start my scheduled task. The error states there’s no running event loop.

Here is my scheduled task that runs daily:

@tasks.loop(hours=24)
async def daily_stock_update():
    url = "https://finance.yahoo.com/quote/^QMI"
    response = requests.get(url)
    soup = BeautifulSoup(response.text, "html.parser")
    stock_info = ""
    try:
        current_price = soup.find_all("div", {"class":"My(6px) Pos(r) smartphone_Mt(6px)"})[0].find_all("span")[0]
        price_change = soup.find_all("div", {"class":"My(6px) Pos(r) smartphone_Mt(6px)"})[0].find_all("span")[1]
        stock_info = f"NASDAQ Pre Market: {current_price.text}, Change: {price_change.text}"
    except:
        stock_info = "No information available"
    
    for guild in bot.guilds:
        for channel in guild.channels:
            try:
                news_embed = get_market_news()
                await channel.send(embed=news_embed)
                await channel.send(stock_info)
            except Exception:
                continue

The error I encounter is:

File "D:\Path\to\bot.py", line 173, in <module>
  daily_stock_update.start()
File "C:\Path\to\venv\Lib\site-packages\discord\ext\tasks\__init__.py", line 398, in start
  self._task = asyncio.create_task(self._loop(*args, **kwargs))
RuntimeError: no running event loop

What can I do to resolve this event loop problem? The task is supposed to activate automatically when the bot starts.

You’re calling daily_stock_update.start() before the bot’s event loop is running. Discord.py needs its event loop established first before creating any async tasks. Move your task initialization inside the on_ready event handler instead of at module level. Here’s what worked for me:

@bot.event
async def on_ready():
    print(f'{bot.user} has connected to Discord!')
    if not daily_stock_update.is_running():
        daily_stock_update.start()

The is_running() check prevents the task from starting multiple times if the bot reconnects. This way, the event loop’s already running when you start your scheduled task. I experienced the same RuntimeError when I tried starting tasks before calling bot.run().

You’re calling daily_stock_update.start() before Discord.py sets up its event loop. No active loop means the coroutine can’t be created. I ran into this same issue with a crypto price tracker bot. You need to start your task after the bot connects and its event loop is running. Since bot.run() blocks everything after it, on_ready is your best bet. If you need more control over timing, use asyncio.create_task() inside an async function. Just remember Discord.py handles its own event loop, so all your async stuff has to work within that.

Had this exact issue last week! You’re trying to start the task before discord.py sets up its event loop. Just wrap your start() call in the on_ready event and ur good to go. Also, add a delay between channels when sending - Discord will rate limit ya if you spam every channel at once.