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.