考虑以下简单的Python脚本:
$ cat test_utc.py
from datetime import datetime
for i in range(10_000_000):
first = datetime.utcnow()
second = datetime.utcnow()
assert first <= second, f"{first=} {second=} {i=}"
当我像python test_utc.py
一样从shell运行它时,它完成了没有错误的操作,正如预期的那样.但是,当我在Docker容器中运行它时,断言失败:
$ docker run -it --rm -v "$PWD":/code -w /code python:3.10.4 python test_utc.py
Traceback (most recent call last):
File "/code/test_utc.py", line 7, in <module>
assert first <= second, f"{first=} {second=} {i=}"
AssertionError: first=datetime.datetime(2022, 5, 24, 19, 5, 1, 861308) second=datetime.datetime(2022, 5, 24, 19, 5, 1, 818270) i=1818860
怎么可能呢?
P、 一位美国同事报告称,将range参数增加到100_000_000
也会使其在mac上的shell中失败(但对我来说不是这样).