如何在Python脚本中设置时间延迟?


当前回答

用一台昏昏欲睡的发电机来找点乐子。

问题是关于时间延迟。它可以是固定的时间,但在某些情况下,我们可能需要从上次开始测量延迟。以下是一种可能的解决方案:

自上次测量的延迟(定期醒来)

情况可能是,我们希望尽可能定期地做一些事情,而不想在代码中使用所有last_time和next_time的东西。

蜂鸣器发生器

以下代码(sleep.py)定义了一个buzzergen生成器:

import time
from itertools import count

def buzzergen(period):
    nexttime = time.time() + period
    for i in count():
        now = time.time()
        tosleep = nexttime - now
        if tosleep > 0:
            time.sleep(tosleep)
            nexttime += period
        else:
            nexttime = now + period
        yield i, nexttime

调用常规蜂鸣器

from sleepy import buzzergen
import time
buzzer = buzzergen(3) # Planning to wake up each 3 seconds
print time.time()
buzzer.next()
print time.time()
time.sleep(2)
buzzer.next()
print time.time()
time.sleep(5) # Sleeping a bit longer than usually
buzzer.next()
print time.time()
buzzer.next()
print time.time()

运行它,我们可以看到:

1400102636.46
1400102639.46
1400102642.46
1400102647.47
1400102650.47

我们也可以在循环中直接使用它:

import random
for ring in buzzergen(3):
    print "now", time.time()
    print "ring", ring
    time.sleep(random.choice([0, 2, 4, 6]))

运行它,我们可能会看到:

now 1400102751.46
ring (0, 1400102754.461676)
now 1400102754.46
ring (1, 1400102757.461676)
now 1400102757.46
ring (2, 1400102760.461676)
now 1400102760.46
ring (3, 1400102763.461676)
now 1400102766.47
ring (4, 1400102769.47115)
now 1400102769.47
ring (5, 1400102772.47115)
now 1400102772.47
ring (6, 1400102775.47115)
now 1400102775.47
ring (7, 1400102778.47115)

正如我们所看到的,这个蜂鸣器并不太死板,即使我们睡过头了,也能赶上规律的睡眠时间。

其他回答

延迟是通过时间库完成的,特别是time.sleep()函数。

让它等待一秒钟:

from time import sleep
sleep(1)

这之所以有效,是因为通过执行以下操作:

from time import sleep

你只从时间库中提取睡眠函数,这意味着你只需要调用它:

sleep(seconds)

而不是必须键入

time.sleep()

打字的时间太长了。

使用此方法,您将无法访问时间库的其他功能,也无法使用名为sleep的变量。但您可以创建一个名为time的变量。

如果您只需要模块的某些部分,那么从[library]导入[function](,[function2])非常有用。

你也可以这样做:

import time
time.sleep(1)

只要输入time,就可以访问时间库的其他功能,如time.clock()。[function](),但无法创建变量time,因为它会覆盖导入。要做的解决方案

import time as t

这将允许您将时间库引用为t,允许您执行以下操作:

t.sleep()

这适用于任何库。

Python标准库中的Tkinter库是一个可以导入的交互式工具。基本上,您可以创建按钮、框、弹出窗口和显示为窗口的东西,并使用代码进行操作。

如果使用Tkinter,请不要使用time.sleep(),因为它会破坏程序。这种情况发生在我身上。相反,使用root.after()并用毫秒来替换多少秒的值。例如,time.sleep(1)相当于Tkinter中的root.after(1000)。

否则,许多答案都指出了time.sleep(),这才是正确的方法。

如何在Python中设置时间延迟?

在一个线程中,我建议使用睡眠功能:

>>> from time import sleep

>>> sleep(4)

该函数实际上暂停了操作系统调用它的线程的处理,允许其他线程和进程在休眠时执行。

为此目的使用它,或者简单地延迟函数的执行。例如:

>>> def party_time():
...     print('hooray!')
...
>>> sleep(3); party_time()
hooray!

“万岁!”在我按下Enter键3秒后打印。

对多个线程和进程使用睡眠的示例

同样,睡眠会暂停线程-它使用几乎为零的处理能力。

为了演示,创建一个这样的脚本(我第一次在交互式Python 3.5 shell中尝试,但由于某些原因,子进程找不到party_later函数):

from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor, as_completed
from time import sleep, time

def party_later(kind='', n=''):
    sleep(3)
    return kind + n + ' party time!: ' + __name__

def main():
    with ProcessPoolExecutor() as proc_executor:
        with ThreadPoolExecutor() as thread_executor:
            start_time = time()
            proc_future1 = proc_executor.submit(party_later, kind='proc', n='1')
            proc_future2 = proc_executor.submit(party_later, kind='proc', n='2')
            thread_future1 = thread_executor.submit(party_later, kind='thread', n='1')
            thread_future2 = thread_executor.submit(party_later, kind='thread', n='2')
            for f in as_completed([
              proc_future1, proc_future2, thread_future1, thread_future2,]):
                print(f.result())
            end_time = time()
    print('total time to execute four 3-sec functions:', end_time - start_time)

if __name__ == '__main__':
    main()

此脚本的输出示例:

thread1 party time!: __main__
thread2 party time!: __main__
proc1 party time!: __mp_main__
proc2 party time!: __mp_main__
total time to execute four 3-sec functions: 3.4519670009613037

多线程

您可以使用Timer线程对象触发稍后在单独线程中调用的函数:

>>> from threading import Timer
>>> t = Timer(3, party_time, args=None, kwargs=None)
>>> t.start()
>>>
>>> hooray!

>>>

空行说明了函数打印到我的标准输出中,我必须按Enter键以确保出现提示。

这种方法的好处是,当Timer线程在等待时,我可以做其他事情,在本例中,在执行函数之前按一下Enter键(见第一个空提示)。

多处理库中没有相应的对象。你可以创建一个,但它可能是没有原因的。对于一个简单的计时器来说,子线程比一个全新的子进程更有意义。

用一台昏昏欲睡的发电机来找点乐子。

问题是关于时间延迟。它可以是固定的时间,但在某些情况下,我们可能需要从上次开始测量延迟。以下是一种可能的解决方案:

自上次测量的延迟(定期醒来)

情况可能是,我们希望尽可能定期地做一些事情,而不想在代码中使用所有last_time和next_time的东西。

蜂鸣器发生器

以下代码(sleep.py)定义了一个buzzergen生成器:

import time
from itertools import count

def buzzergen(period):
    nexttime = time.time() + period
    for i in count():
        now = time.time()
        tosleep = nexttime - now
        if tosleep > 0:
            time.sleep(tosleep)
            nexttime += period
        else:
            nexttime = now + period
        yield i, nexttime

调用常规蜂鸣器

from sleepy import buzzergen
import time
buzzer = buzzergen(3) # Planning to wake up each 3 seconds
print time.time()
buzzer.next()
print time.time()
time.sleep(2)
buzzer.next()
print time.time()
time.sleep(5) # Sleeping a bit longer than usually
buzzer.next()
print time.time()
buzzer.next()
print time.time()

运行它,我们可以看到:

1400102636.46
1400102639.46
1400102642.46
1400102647.47
1400102650.47

我们也可以在循环中直接使用它:

import random
for ring in buzzergen(3):
    print "now", time.time()
    print "ring", ring
    time.sleep(random.choice([0, 2, 4, 6]))

运行它,我们可能会看到:

now 1400102751.46
ring (0, 1400102754.461676)
now 1400102754.46
ring (1, 1400102757.461676)
now 1400102757.46
ring (2, 1400102760.461676)
now 1400102760.46
ring (3, 1400102763.461676)
now 1400102766.47
ring (4, 1400102769.47115)
now 1400102769.47
ring (5, 1400102772.47115)
now 1400102772.47
ring (6, 1400102775.47115)
now 1400102775.47
ring (7, 1400102778.47115)

正如我们所看到的,这个蜂鸣器并不太死板,即使我们睡过头了,也能赶上规律的睡眠时间。

这将延迟2.5秒:

import time
time.sleep(2.5)

下面是另一个例子,其中某个东西大约每分钟运行一次:

import time
while True:
    print("This prints once a minute.")
    time.sleep(60) # Delay for 1 minute (60 seconds).