Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Time Delays

Tags:

python

time

I want to know how to call a function after a certain time. I have tried time.sleep() but this halts the whole script. I want the script to carry on, but after ???secs call a function and run the other script at the same time

like image 281
drnessie Avatar asked Aug 08 '10 08:08

drnessie


People also ask

How can I make a time delay in Python?

sleep() function. The standard way to add a time delay in Python is by calling the sleep() function from the time module. It works by suspending the execution of the calling thread for the specified number of seconds, which may be a floating-point value.

How do you wait 10 seconds in Python?

If you've got a Python program and you want to make it wait, you can use a simple function like this one: time. sleep(x) where x is the number of seconds that you want your program to wait.

How do you wait 0.5 seconds in Python?

Use time. This will sleep for half of a second.


1 Answers

Have a look at threading.Timer. It runs your function in a new thread.

from threading import Timer  def hello():     print "hello, world"  t = Timer(30.0, hello) t.start() # after 30 seconds, "hello, world" will be printed 
like image 103
Mark Byers Avatar answered Sep 26 '22 14:09

Mark Byers