#Any ideas how i would improve this snippet

1 messages · Page 1 of 1 (latest)

chrome harness
#

for context msg[0] is msg id, msg[2] is for vote number

#

also to be more specific this line is the problem

        msg_object = await theme_vote.fetch_message(msg[0])
lucid pawn
#

You haven't shown any of your SQL queries, or what team_vote is. So it's impossible for anyone to know what is causing the time

chrome harness
#

ok srry

#

vote_values is a list

#

and here is the sql querry

#

vote_values = cursor.execute ( "SELECT * FROM theme_msgs WHERE For_Jam = ? ORDER BY Amount_Votes DESC LIMIT 10", [jam]).fetchall()

#

jam is just a string

lucid pawn
#

And what is theme_vote? Is it a text channel?

#

Eh whatever, doesn't really matter. Instead of looping over vote_values and waiting for fetch_message to complete in every iteration of the loop. You can use asyncio.gather to run multiple coroutines concurrently that will then return their values into a list.

Example

results = await asyncio.gather(some_coro(), another_coro())
print(results)
# ['Hello', 'world']
#

assuming some_coro returns 'Hello' and another_coro returns 'world'

#

Doing this should realistically cut your time down to ~0.33s

chrome harness
chrome harness
#

0.33s isn't a huge time

#

as it takes apparently not 1 entire second

#

but 3 seconds

lucid pawn
#

Either way it's going to be a lot faster to use gather

chrome harness
#

ok

lucid pawn
#

And another thing you should do is try to use get_message first to see if the message is cached. If that returns None, use fetch_message

chrome harness
#

but wait

#

msg_object = await theme_vote.fetch_message(msg[0]) needs to be executed first

#

so it can get without error msg_object.embeds[0].title

#

oh wait

#

im dumb its basically useless to do fetch

#

yeh i did optimise it

#

and runs better