Windows 中的 datetime.now() vs WSL vs Linux ?

问题描述 投票:0回答:1

我发现 Windows Python 3.11 与 WSL Python 3.10 和 Linux Python 3.10 中的 datetime.now() 函数存在差异。在 Windows 上,我收到重复的条目。在 WSL 和 Linux 上,我不知道。

我正在将测试系统从主要在 WSL 中与 Python 一起使用转换为在 Windows 中本机使用 Python,并遇到了一种奇怪的问题。在一个地方,前一作者在脚本开始时记录了 datetime.now() ,然后检查它的增量,看看它是否仍然相同(我完全不确定为什么,但这就是发生的事情)。这突然与 Windows 中的 Python 决裂了。经过一番挖掘,似乎这是因为大多数时候两个 datetime.now() 调用实际上没有增量。我用 WSL 尝试过,仍然有效。这是我在胚芽中谈论的一个例子:

from datetime import datetime as dt
for i in range(0,10):
    print(f"date and time is: {dt.now()}")

对于 Windows,打印出来的内容如下:

date and time is: 2024-02-26 17:09:39.393765
date and time is: 2024-02-26 17:09:39.393765
date and time is: 2024-02-26 17:09:39.408956
date and time is: 2024-02-26 17:09:39.408956
date and time is: 2024-02-26 17:09:39.408956
date and time is: 2024-02-26 17:09:39.409962
date and time is: 2024-02-26 17:09:39.409962
date and time is: 2024-02-26 17:09:39.409962
date and time is: 2024-02-26 17:09:39.409962
date and time is: 2024-02-26 17:09:39.410971

如您所见,多个条目是相同的。

与 WSL 相同的系统和代码:

date and time is: 2024-02-26 17:10:58.658753
date and time is: 2024-02-26 17:10:58.658802
date and time is: 2024-02-26 17:10:58.658828
date and time is: 2024-02-26 17:10:58.658837
date and time is: 2024-02-26 17:10:58.658857
date and time is: 2024-02-26 17:10:58.658880
date and time is: 2024-02-26 17:10:58.658888
date and time is: 2024-02-26 17:10:58.658892
date and time is: 2024-02-26 17:10:58.658911
date and time is: 2024-02-26 17:10:58.658935

没有重复。

我最初的假设是这是因为 WSL 作为虚拟机的开销,但我不太确定。我只是觉得很奇怪,并且在网上找不到任何帮助,因为人们通常遇到 datetime.now() “不更新”的原因似乎是因为他们将输出分配给变量而不是重新运行它,但我每次都会再次运行。

我很好奇,所以我只是在我设置的 Linux 机器上再次运行它(Ubuntu 22.04),它也从未看到任何重复项。

date and time is: 2024-02-27 09:38:16.767813
date and time is: 2024-02-27 09:38:16.767853
date and time is: 2024-02-27 09:38:16.767860
date and time is: 2024-02-27 09:38:16.767864
date and time is: 2024-02-27 09:38:16.767869
date and time is: 2024-02-27 09:38:16.767873
date and time is: 2024-02-27 09:38:16.767878
date and time is: 2024-02-27 09:38:16.767882
date and time is: 2024-02-27 09:38:16.767889
date and time is: 2024-02-27 09:38:16.767894

我检查了每个版本中的 Python 版本,确实发现了差异: Windows:Python 3.11.8 WSL:Python 3.10.12 Ubuntu:Python 3.10.12

CPU 也不同,如果这在这种情况下很重要的话:

Windows: i7-8700 CPU @ 3.20GHz、3192 Mhz、6 核、12 个逻辑处理器

Linux: LSCPU 架构:x86_64 CPU 操作模式:32 位、64 位 地址大小:39 位物理地址,48 位虚拟地址 字节顺序:小端 CPU:8 在线 CPU 列表:0-7 供应商 ID:GenuineIntel 型号名称:Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz CPU系列:6 型号:94 每核线程:2 每个插槽的核心数:4 插座:1 步数:3 CPU 最大 MHz:3400.0000 CPU 最低 MHz:800.0000

通过添加 time.sleep(0.000001) 很容易让我正在使用的代码与 Windows 的 Python 一起使用,我不确定是否需要有问题的代码,但我只是好奇为什么这是这样正在发生,想知道是否有人可以阐明它。

谢谢!

python linux windows windows-subsystem-for-linux
1个回答
0
投票

由于 Python 使用底层操作系统功能,因此它的一些行为会根据运行时环境所支持的操作系统而有所不同。虽然您的测试因使用不同的 Python 版本而存在一些缺陷,但统一这些版本很可能会产生类似的结果。

Windows 历史上一直存在时间粒度问题,时钟更新频率不如某些 Linux 配置高。时间测量的精度和系统时间更新的分辨率可能会有很大差异,导致

datetime.now()
返回新值的频率存在差异。在脚本中添加
time.sleep(0.000001)
可能会强制 Python 解释器产生足够长的执行时间以便系统时钟更新,从而避免重复。这种解决方法通常是可以接受的,但应谨慎使用,要理解它会给每次循环迭代带来少量开销,如果不需要如此详细的时间粒度,则可能不需要这种开销。

© www.soinside.com 2019 - 2024. All rights reserved.