Alright, I want to know how to delay a portion of a program without pausing the entire program. I\'m not necessarily good at python so if you could give me a relatively simple a
You need to put the part of the program you want to delay in its own thread, and then call sleep() in that thread.
I am not sure exactly what you are trying to do in your example, so here is a simple example:
import time
import threading
def print_time(msg):
print 'The time %s is: %s.' % (msg, time.ctime(time.time()))
class Wait(threading.Thread):
def __init__(self, seconds):
super(Wait, self).__init__()
self.seconds = seconds
def run(self):
time.sleep(self.seconds)
print_time('after waiting %d seconds' % self.seconds)
if __name__ == '__main__':
wait_thread = Wait(5)
wait_thread.start()
print_time('now')
Output:
The time now is: Mon Jan 12 01:57:59 2015.
The time after waiting 5 seconds is: Mon Jan 12 01:58:04 2015.
Notice that we started the thread that will wait 5 seconds first, but it did not block the print_time('now') call, rather it waited in the background.
EDIT:
From J.F. Sebastian's comment, the simpler solution with threading is:
import time
import threading
def print_time(msg):
print 'The time %s is: %s.' % (msg, time.ctime(time.time()))
if __name__ == '__main__':
t = threading.Timer(5, print_time, args = ['after 5 seconds'])
t.start()
print_time('now')
There is turtle.ontimer() that calls a function with the specified delay:
turtle.ontimer(your_function, delay_in_milliseconds)