python操作系统统计().st_mtime不刷新/proc/moun的最后修改

2024-06-01 00:28:38 发布

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

试图理解从python文件中提取最后修改时间时的一些有趣行为。似乎python没有正确地刷新st_mtime(从某些文件),或者我做了一些非常错误的事情。在

我的环境:

# python36 
Python 3.6.1 (default, May 11 2017, 13:02:16) 
[GCC 6.3.1 20161221 (Red Hat 6.3.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import datetime # this is just to see when was the command executed

我的问题是:

^{pr2}$

如你所见,我正在跑步操作系统统计('/proc/mounts').st_mtime命令,时间间隔约为2秒,但/proc/mounts的最后修改时间保持不变(epoch时间为静态1502135080.920084)

只是为了证明python能够提取其他类似文件的最后修改时间:

>>> print(datetime.datetime.now(), " --- ", os.stat('/proc/vmstat').st_mtime)
2017-08-07 22:57:30.064103  ---  1502135850.0637884
>>> print(datetime.datetime.now(), " --- ", os.stat('/proc/vmstat').st_mtime)
2017-08-07 22:57:32.126273  ---  1502135852.1257987
>>> print(datetime.datetime.now(), " --- ", os.stat('/proc/vmstat').st_mtime)
2017-08-07 22:57:34.409254  ---  1502135854.4078097

在上面的例子中,另一个文件的最后修改时间被正确地更新了。在

我要检查的文件是/proc/mounts,它(至少在我的例子中)是指向/proc/self/mounts的符号链接,但是即使我试图提取实际文件的元数据,最后的修改仍然没有更新。在

还有一件事:这个文件确实更新了(由内核?)非常频繁。这里有一个小的外壳,用来证明文件的最后修改日期正在不断更新:

# [130] > while true; do ls -l --time-style=full-iso /proc/mounts; sleep 1; done
lrwxrwxrwx. 1 root root 11 2017-08-07 23:02:37.139422559 +0300 /proc/mounts -> self/mounts
lrwxrwxrwx. 1 root root 11 2017-08-07 23:02:38.143428493 +0300 /proc/mounts -> self/mounts
lrwxrwxrwx. 1 root root 11 2017-08-07 23:02:39.148434433 +0300 /proc/mounts -> self/mounts
lrwxrwxrwx. 1 root root 11 2017-08-07 23:02:40.152440367 +0300 /proc/mounts -> self/mounts

对于python提取/proc/mounts的最后一次修改时间会发生什么,有什么建议吗?在我看来,python正在缓存这个值。在


Tags: 文件selfdatetimeos时间rootprocnow