#๐Ÿ”’ How do I pause execution for a given time (like sleep()) but taking into account execution time?

29 messages ยท Page 1 of 1 (latest)

spare mango
#

Let's say my code runs anywhere between .05 seconds and .5 (it's not that variant but you get it), and I want to start each cycle 1 second apart. How do I take into account the amount of time it takes?

torpid valveBOT
#

@spare mango

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

jaunty quarry
spare mango
#

ooh

jaunty quarry
#

then subtract that from 1 second, and sleep for that amount

spare mango
#

smart

#

will do

#

what format is time in again?

#

one sec

jaunty quarry
#

it's epoch time

#

it's a float of seconds

#

!e

import time

print(time.time())
torpid valveBOT
spare mango
#

alright perfect

jaunty quarry
#

exactly

#

but that isn't necessarily important

#

you just need to get the difference of 2 different time.time() calls

#

!e

import time

start = time.time()
time.sleep(1)
end = time.time()

print(f"The process took {end - start:.2f} seconds")
torpid valveBOT
chrome wharf
spare mango
spare mango
#

I've gotten a good answer, let's see if this can top it lol

chrome wharf
#

Timer calls your function after a given time

#

maybe not the best fit to use threads tho, idk

spare mango
#

I may need threading for performance later in my code, I'll check it out.

torpid valveBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.