繁体   English   中英

Python os.listdir() 不返回某些文件

[英]Python os.listdir() doesn't return some files

我有一个 python function 使用os.stat()检查文件夹的修改时间,然后它对该文件夹执行os.listdir()

我有另一个进程在被检查的文件夹中创建文件。 有时会观察到三个文件使用相同的时间戳创建,并且文件夹的 stat 也具有相同的时间戳。

当 python function 在同一毫秒内获取文件时,观察到os.listdir()仅提供 3 个创建文件中的 2 个。 为什么会这样?

环境是:

操作系统:红帽企业 Linux 服务器版本 7.6

Python 版本:Python 3.6.8

文件系统:xfs

示例代码

import os
import sys
import time

filelist=list()
last_mtime=None


def walkover():
    path_to_check="/path/to/check"
    curr_mtime=os.stat(path_to_check).st_mtime_ns
    global last_mtime
    global filelist
    if last_mtime == None or curr_mtime > last_mtime:
        for file in os.listdir(path_to_check):
            if file not in filelist:
                filelist.append(file)
        last_mtime = curr_mtime
        print ("{} modified at {}".format(path_to_check, last_mtime))


调用 function 来维护某个时间点的文件列表。 存在 if 情况以避免多次os.listdir()调用。

编辑:

这些文件是“.rsp”文件,当“.o”即将构建时由忍者创建。

由于我的机器有多个内核(16),忍者是从 cmake 用“--parallel 16”触发的。 这将导致 16 次编译并行发生。

我不知道这个忍者的事情,但在我看来,当编写第一个“.rst”文件并运行 walkover function 时,几乎同时执行 walkover function 可能会在创建其他文件时丢失。 无论是缓冲区还是队列或其他什么,特别是当并行进程正在运行时,事件的顺序可能很难掌握。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM