在两个延迟日期时间输出之间添加毫秒

2024-03-28 10:42:59 发布

您现在位置:Python中文网/ 问答频道 /正文

代码:

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


Tags: 代码truedatetimetimesleepnowprintwhile
1条回答
网友
1楼 · 发布于 2024-03-28 10:42:59

{a1}文档是最好的:

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.

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

相关问题 更多 >