Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Why does import time not work for time() but works for time.sleep()?

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'

like image 421
Nav Avatar asked Sep 16 '12 14:09

Nav


People also ask

Is time sleep blocking?

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.

How does time sleep work in Python?

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.

Is Python time sleep accurate?

sleep function converts a python float into a 32 bit c float and that loses accuracy.

How do you wait 0.5 seconds in Python?

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.


1 Answers

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)
>>>
like image 61
DSM Avatar answered Oct 21 '22 01:10

DSM