How to keep track of time in python without sleeping?

703 Views Asked by At

I'm wondering how I can play a .wav file after some time has passed without using the sleep function. Essentially, I was wondering if there is a way to keep track of time in Python, so that, after say 15 seconds has elapsed, I can play a sound without pausing my code.

# checks if I should play the sound or not and, sets the variable

def Tyler(self):
    if self.started == 1:
        if self.isTaiwan == 0:
            if self.myListNames[self.current_player_id / 3].lower() == "tyler":
                self.isTyler = 1
            else:
                self.isTyler = 0

self.Tyler()

if self.isTyler == 1:
    time.sleep(6)
    winsound.PlaySound("tyler.wav", winsound.SND_ASYNC)

# This is where I would want to check to see 
# if some time has passed and the conditions haven't changed.
1

There are 1 best solutions below

0
On
from time import time
def delay(secs):
    init_time = time()
    while time() < init_time+secs: pass

This won't use sleep but it is similar. Still uses time module