Issue
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 insider
@client.event
async def on_ready():
print("Bot is Ready.")
check_insider_activity.start()
@tasks.loop(minutes=5)
async def check_insider_activity():
print("Checking Insider Buys")
newBuys = insider.get_new_insider_buys()
print(newBuys)
Whenever I run my code, the output is as follows :
Bot is ready.
Checking Insider Buys
Checking Insider Buys
Checking Insider Buys
Checking Insider Buys
Checking Insider Buys
Checking Insider Buys
It sends "Checking Insider Buys" roughly every second or two, and keeps on doing that until I exit the program. The function get_new_insider_buys() never seems to complete, and I never get to the print statement before the task seems to just restart.
For some context, get_new_insider_buys() is a function which returns a list of new insider buys. The function scrapes a webpage using BeautifulSoup and manipulates some data using a Pandas Dataframe, then writes it to a local file before returning a list.
get_new_insider_buys() 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.
Solution
I managed to fix my issue.
It was happening as a byproduct of the f = open("filepath", "w")
within my insider.get_new_insider_buys()
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.
Answered By - Fluxify
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.