I did a top command on discord.py (using Replit database). Basically shows the top 10 users with the most XP in the server.
That's how a part of my code looks like, ignore my messy way of saving keys to the database
@commands.command()
async def top(self,ctx,nr:int=1):
match_keys = db.prefix(f"{ctx.guild.id}XP")
sort = sorted(match_keys, key=lambda x: db[x],reverse=True)
[...]
I have like 2000 key to sort, this whole thing takes about 10 seconds to finish. In those 10 seconds, the bot is unusable. I have tested out and the only thing that takes time is the sort. I saw other similar questions on StackOverflow but from what I found there, the sort/sorted method is based on quicksort, I can't really do much about it to be faster.
I am thinking of making a task loop which will update the top once in a while (less time to wait when running the .top command), but the bot will be unusable while it sorts the list.
Is it possible to slow down the sort so I could use the bot while the operation? Thanks!
sorry for potential mistakes, I'm new to stackoverflow