Python相对时钟循环

2024-06-10 10:26:13 发布

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

假设我们有一个用Python编写的日志记录功能(可能不相关,但仍然如此),并且日志记录还记录了日志中的时间。在

然后在never-ending while循环中经过一段时间后,有人更改了系统时钟(由系统本身更改,也可以由sysadmin更改)。当然,所有的计时都会在日志中出错(特别是如果结果是时钟已经被退回了一段时间)。在

有没有什么方法可以通过在whileloop中拥有自己的相对时钟来防止这种情况发生?我想记录循环之前的时间,然后测量循环中的迭代时间,并手动将其添加到节省的时间中,但当然,过了一段时间后,这种情况会变得非常糟糕。在

有什么建议吗?在


Tags: 方法功能系统ending记录时间情况手动
1条回答
网友
1楼 · 发布于 2024-06-10 10:26:13

如果您在UNIX系统下工作,os.times()不会受到系统时间更改的影响:

os.times()

Return a 5-tuple of floating point numbers indicating accumulated (processor or other) times, in seconds. The items are: user time, system time, children’s user time, children’s system time, and elapsed real time since a fixed point in the past, in that order. See the Unix manual page times(2) or the corresponding Windows Platform API documentation. On Windows, only the first two items are filled, the others are zero.

>>> import os
>>> os.times()
(0.01, 0.0, 0.0, 0.0, 17194492.75)
>>> os.times()[4]
17194500.18

相关问题 更多 >