I'm currently running into an issue with Discord.py where I have a task loop which keeps reiterating instantly without actually finishing. Below you can find a snippet of my code.
import discord
from discord.ext import commands, tasks
import utils
@client.event
async def on_ready():
print("Bot is Ready.")
my_task.start()
@tasks.loop(minutes=5)
async def my_task():
print("Starting my_task")
output = utils.generate_output()
print(output)
Whenever I run my code, the output is as follows :
Bot is ready.
Starting my_task
Starting my_task
Starting my_task
Starting my_task
Starting my_task
Starting my_task
It sends "Starting my_task" roughly every second or two, and keeps on doing that until I exit the program. The function generate_output() never seems to complete, and I never get to the print(output) statement before the task seems to just restart.
For some context, generate_output() is a function which returns a list of sources from a news page. The function scrapes the webpage using BeautifulSoup and manipulates some data using a Pandas Dataframe, then writes it to a local file before returning a list.
my_task() runs perfectly on its own, but as soon as I want to put it into the Discord.py task loop, it never seems to execute.
I feel as though I've got some form of concurrency or task error happening within the function, though I don't know how or why, and I'm at my wits end. I can get other tasks working just fine, but as soon as I add this one to the mix, it all breaks, and all of my tasks start behaving like this one.
I managed to fix my issue.
It was happening as a byproduct of the f = open("filepath", "w")
within my utils.generate_output()
function. I had a relative path for my file path, not a direct path. I guess the way open() handles those is different, and as a result my program was bugging out.
I hope those who stumble upon this get saved the headache.