简体   繁体   English

Python的time.sleep()方法等待不正确的时间

[英]Python's time.sleep() method waits incorrect amount of time

I've run into this problem a few times; 我曾经遇到过这个问题几次; restarting python seems to work (or ipython). 重新启动python似乎可以工作(或ipython)。 But, for instance, here's one possible output of running the following code: 但是,例如,这是运行以下代码的一种可能的输出:

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

I obtain: 我获得:

9.989
10.989
11.990
12.991

Why does it wait so long before it begins working? 为什么它在开始工作之前等待这么久? Occasionally, it will start at 10 or even 11 seconds after I run the command. 偶尔,它会在我运行命令后的10秒甚至11秒开始。

I'm using Mac OS X (Mavericks), IPython 1.2.1 (with pylab), Python 2.7.5 我正在使用Mac OS X(Mavericks),IPython 1.2.1(使用pylab),Python 2.7.5

I'm importing: os, cv2, time, random, Quartz, LaunchServies, pdb, sys, appscript, and numpy. 我正在导入:os,cv2,time,random,Quartz,LaunchServies,pdb,sys,appscript和numpy。

As per the time.sleep docs : 根据time.sleep docs

Suspend execution for the given number of seconds. 暂停执行指定的秒数。 The argument may be a floating point number to indicate a more precise sleep time. 参数可以是浮点数,以指示更精确的睡眠时间。 The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal's catching routine. 实际的暂停时间可能小于请求的时间,因为任何捕获的信号将在执行该信号的捕获例程后终止sleep()。 Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system. 而且,由于系统中其他活动的调度,暂停时间可能比请求的时间长任意数量。

The actual wait time of time.sleep is not guaranteed and depends on how the host system is loaded. time.sleep的实际等待时间无法保证,取决于主机系统的加载方式。 If something on your machine gets hold of the resources the Python process may get delayed until resumed. 如果您机器上的某些东西获取了资源,Python进程可能会被延迟直到恢复。

Still, the delay of seconds is just too high. 但是,秒延迟还是太高了。 Are you by any chance trying this in the Python interactive shell? 您是否有机会在Python交互式shell中尝试此操作? If so, it may interfere, eg: 如果是这样,它可能会干扰,例如:

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

Because the startt = time.time() gets evaluated before the rest of the code gets written or pasted in and evaluated, which can take seconds. 因为startt = time.time()在其余代码被写入或粘贴并评估之前得到评估,这可能需要几秒钟。

But it behaves as expected if I wrap it in a method: 但是如果我将它包装在一个方法中,它的行为就像预期的那样:

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

or put into a script: 或者放入一个脚本:

import time

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

# $ python test.py
# 1.000
# 2.008
# 3.008
# 4.008

In this case the delays should be in order of milliseconds, as can be seen in the latter output. 在这种情况下,延迟应该是毫秒级,如后一输出中所示。 I doubt it could get to seconds. 我怀疑它可以达到秒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM