Python's time.sleep() method waits incorrect amount of time -


i've run problem few times; restarting python seems work (or ipython). but, instance, here's 1 possible output of running following code:

startt = time.time() in range(4):     time.sleep(1)     print '%.3f'%(time.time()-startt) 

i obtain:

9.989 10.989 11.990 12.991 

why wait long before begins working? occasionally, start @ 10 or 11 seconds after run command.

i'm using mac os x (mavericks), ipython 1.2.1 (with pylab), python 2.7.5

i'm importing: os, cv2, time, random, quartz, launchservies, pdb, sys, appscript, , numpy.

as per time.sleep docs:

suspend execution given number of seconds. argument may floating point number indicate more precise sleep time. actual suspension time may less requested because caught signal terminate sleep() following execution of signal’s catching routine. also, suspension time may longer requested arbitrary amount because of scheduling of other activity in system.

the actual wait time of time.sleep not guaranteed , depends on how host system loaded. if on machine gets hold of resources python process may delayed until resumed.

still, delay of seconds high. chance trying in python interactive shell? if so, may interfere, e.g.:

>>> import time >>> startt = time.time() >>> in range(4): ...     time.sleep(1) ...     print '%.3f'%(time.time()-startt) ... 3.147 4.147 5.147 6.147 >>> startt = time.time() >>> in range(4): ...     time.sleep(1) ...     print '%.3f'%(time.time()-startt) ... 4.949 5.949 6.949 7.949 

because startt = time.time() gets evaluated before rest of code gets written or pasted in , evaluated, can take seconds.

but behaves expected if wrap in method:

>>> def test(): ...     startt = time.time() ...     in range(4): ...         time.sleep(1) ...         print '%.3f'%(time.time()-startt) ... >>> test() 1.000 2.000 3.000 4.000 

or put script:

import time  startt = time.time() in range(4):     time.sleep(1)     print '%.3f'%(time.time()-startt)  # $ python test.py # 1.000 # 2.008 # 3.008 # 4.008 

in case delays should in order of milliseconds, can seen in latter output. doubt seconds.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -