【问题标题】:Miliseconds added between the two delayed datetime outputs两个延迟的日期时间输出之间添加的毫秒数
【发布时间】:2014-11-17 23:54:26
【问题描述】:

代码:

while True:
    print(datetime.datetime.now())
    time.sleep(5)
    print(datetime.datetime.now())

结果:

00:06:53.728000

00:06:58.763000

00:06:58.833000

00:07:03.838000

那么为什么不显示为:06:58.728000、06:58.728000、07:03:728000?

【问题讨论】:

标签: python datetime python-3.x


【解决方案1】:

time.sleep 文档是最好的:

实际的挂起时间可能少于请求的时间,因为任何捕获的信号都会在执行该信号的捕获例程后终止 sleep()。

还有:

此外,由于系统中其他活动的调度,暂停时间可能比请求的时间长任意数量。

所以,没有任何保证(这在过去也让我绊倒了)。您将获得 5 秒的延迟(大约),但实际延迟可能会比请求的延迟少和/或多一点。

【讨论】:

    猜你喜欢
    • 2018-10-15
    • 2023-01-19
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    • 2020-12-12
    • 2011-10-25
    • 2021-10-26
    相关资源
    最近更新 更多