Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

usleep in Python

Tags:

python

I was searching for a usleep() function in Python 2.7.

Does anybody know if it does exist, maybe with another function name?

like image 949
Federico Zancan Avatar asked Apr 06 '11 15:04

Federico Zancan


People also ask

How do you wait 0.5 seconds in Python?

You Can Be Precise Make your time delay specific by passing a floating point number to sleep() . from time import sleep print("Prints immediately.") sleep(0.50) print("Prints after a slight delay.")

How do you wait 1 second 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 sleep microseconds Python?

To sleep for milliseconds with this method, simply use a fractional number. To sleep for 400 milliseconds, for example, use time. sleep(0.4), use time for 60 milliseconds sleep(0.06), for example.


2 Answers

Since usleep generally means you want to delay execution for x microseconds, you must divide the seconds value by 1000000.

import time time.sleep(seconds/1000000.0) 

time.sleep() takes seconds as a parameter.

http://docs.python.org/library/time.html#time.sleep

like image 76
Mike Lewis Avatar answered Sep 29 '22 01:09

Mike Lewis


import time usleep = lambda x: time.sleep(x/1000000.0)  usleep(100) #sleep during 100μs 
like image 42
Fábio Diniz Avatar answered Sep 29 '22 03:09

Fábio Diniz