以下測驗總是失敗(這在 linux 系統上運行,問題與其他作業系統無關):
from time import time
from decimal import Decimal
from pathlib import Path
def test_timing():
start = Decimal(time())
p = Path(__file__).parent / 'testfile.txt' # does not yet exist
p.touch()
mt = p.stat().st_mtime
p.unlink() # unlinked before the failing assert
> assert start <= mt
E AssertionError: assert Decimal('1640930671.75709438323974609375') <= 1640930671.7534654
間隔總是大約 3 到 7 毫秒。
怎么可能Decimal(time())在開始時回傳的時間戳晚于在其后兩行創建的檔案?
python時間戳和linux的時間戳之間有偏移嗎?python 是否在Decimal(time())呼叫完成之前繼續創建檔案?我在這里錯過了什么?
編輯:我應該提到它是 ext4 檔案系統。
uj5u.com熱心網友回復:
ext4 檔案系統用于current_fs_time獲取內核時間戳。為了效率,內核時間戳是一個快取值,它只在調度程式間隔更新,大約是 10ms。因此,當它被存盤時,時間可能長達 10 毫秒。
作為旁注,將時間戳轉換為Decimal. 來自 C 的值是一個雙精度值,它只包含大約 17 位數字。這意味著您可以精確到微秒。轉換為Decimal不會獲得任何額外的精度;其他數字基本上是隨機的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/399328.html
標籤:Python 蟒蛇-3.x linux 分机4 文件时间
上一篇:如何一次獲取所有名稱鍵
