Python 的 time.sleep() 方法等待的时间不正确 [英] Python's time.sleep() method waits incorrect amount of time

查看:88
本文介绍了Python 的 time.sleep() 方法等待的时间不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题我遇到过几次了;重新启动 python 似乎工作(或 ipython).但是,例如,这是运行以下代码的一种可能输出:

startt = time.time()对于范围内的 i (4):时间.睡眠(1)打印 '%.3f'%(time.time()-startt)

我得到:

9.98910.98911.99012.991

为什么要等这么久才开始工作?有时,它会在我运行命令后 10 秒甚至 11 秒启动.

我使用的是 Mac OS X(Mavericks)、IPython 1.2.1(带有 pylab)、Python 2.7.5

我正在导入:os、cv2、时间、随机、Quartz、LaunchServies、pdb、sys、appscript 和 numpy.

解决方案

根据 time.sleep 文档:

<块引用>

暂停执行给定的秒数.参数可能是一个浮点数,以指示更精确的睡眠时间.实际的暂停时间可能比请求的要少,因为任何捕获的信号都会在执行该信号的捕获例程后终止 sleep().此外,由于系统中其他活动的安排,暂停时间可能比任意数量的请求长.

time.sleep 的实际等待时间无法保证,取决于主机系统的加载方式.如果您机器上的某些东西占用了资源,Python 进程可能会延迟,直到恢复.

不过,秒的延迟实在是太高了.你有机会在 Python 交互式 shell 中尝试这个吗?如果是这样,它可能会干扰,例如:

<预><代码>>>>导入时间>>>开始 = time.time()>>>对于范围内的 i (4):... time.sleep(1)... 打印 '%.3f'%(time.time()-startt)...3.1474.1475.1476.147>>>开始 = time.time()>>>对于范围内的 i (4):... time.sleep(1)... 打印 '%.3f'%(time.time()-startt)...4.9495.9496.9497.949

因为 startt = time.time() 在编写或粘贴其余代码并对其进行评估之前得到评估,这可能需要几秒钟.

但是如果我将它包装在一个方法中,它的行为就会像预期的那样:

<预><代码>>>>定义测试():... startt = time.time()...对于范围内的我(4):... time.sleep(1)... 打印 '%.3f'%(time.time()-startt)...>>>测试()1.0002.0003.0004.000

或放入脚本:

导入时间开始 = time.time()对于范围内的 i (4):时间.睡眠(1)打印 '%.3f'%(time.time()-startt)# $ python test.py# 1.000# 2.008# 3.008# 4.008

在这种情况下,延迟应该以毫秒为单位,如后面的输出所示.我怀疑它可以达到几秒钟.

I've run into this problem a few times; restarting python seems to work (or 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.

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, and numpy.

解决方案

As per the 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. 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. If something on your machine gets hold of the resources the Python process may get delayed until resumed.

Still, the delay of seconds is just too high. Are you by any chance trying this in the Python interactive shell? If so, it may interfere, e.g.:

>>> 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.

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.

这篇关于Python 的 time.sleep() 方法等待的时间不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆