If I use from time import time
, the Python 2.7.3 does not recognize time.sleep(60)
. But if I use import time
, then Python does not recognize t=time()
. Why does this happen? Is there any way I can use time()
and time.sleep(x)
in the same program?
from time import time
#import time
intervalInMinute = 1
t = time()
while 1:
time.sleep(60)
The kind of error I get is:
Traceback (most recent call last): File "myProg.py", line 9, in time.sleep(60) AttributeError: 'builtin_function_or_method' object has no attribute 'sleep'
The reason you'd want to use wait() here is because wait() is non-blocking, whereas time.sleep() is blocking. What this means is that when you use time.sleep() , you'll block the main thread from continuing to run while it waits for the sleep() call to end. wait() solves this problem.
What Is Sleep() in Python? The sleep() function in python's time module is used to suspend the execution of a program for the given number of seconds. This means that the program is paused for the given time and after that time has passed, the program gets executed automatically.
sleep function converts a python float into a 32 bit c float and that loses accuracy.
You Can Be Precise from time import sleep print("Prints immediately.") sleep(0.50) print("Prints after a slight delay.") This is what happened when I ran the code: "Prints immediately." This line printed immediately. Then, there was a delay of approximately 0.5 seconds.
You need to decide what you want the name time
to refer to, the module or the function called time
in the module. You can write:
>>> from time import time, sleep
>>> time()
1347806075.148084
>>> sleep(3)
>>>
or
>>> import time
>>> time.time()
1347806085.739065
>>> time.sleep(2)
>>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With